首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >批处理文件,以比较具有不同数据的两个不同文件。

批处理文件,以比较具有不同数据的两个不同文件。
EN

Stack Overflow用户
提问于 2012-03-23 07:21:05
回答 2查看 1.9K关注 0票数 0

我想比较这两个文件,如果数据不匹配,则打印一条消息“数据不一样”,如果它们匹配成功,则打印“数据是相同的”。

第一文件(Live.txt)的内容:

代码语言:javascript
运行
复制
Last
4000
5000
(2 Row affected)

内容第二文件(Sup.txt):

代码语言:javascript
运行
复制
Last
3000
6000
(2 Row affected)

OS: Windows7

EN

回答 2

Stack Overflow用户

发布于 2012-03-23 08:59:48

在Microsoft you can use fc command上。

在Linux和类似系统上

代码语言:javascript
运行
复制
cmp <file1> <file2>

将告诉您文件是否不同,并且:

代码语言:javascript
运行
复制
diff <file1> <file2>

会显示出不同之处。

票数 1
EN

Stack Overflow用户

发布于 2013-12-31 04:25:54

我们还可以用特定的布局逐字节编写大文件,并填充excel中的差异。

代码语言:javascript
运行
复制
import java.awt.image.SampleModel;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class FlatFileComparator {

    /*
     * Get the three flat files.
     * 
     * One for Layout, Second for Expected File Third for Actual file
     */
    public static void main(String args[]) throws Exception {
        String fileName = "recordLayout.txt";
        String actualFileName = "Actual.txt";
        String expectedFileName = "Expected.txt";
        List<String> recordLayout = null;
        FlatFileComparator fb = new FlatFileComparator();
        recordLayout = fb.getFileLayout(fileName);
        fb.compareExpectedActual(actualFileName, expectedFileName, recordLayout);

    }

    // Get the Record Names of the Layout and put it in the List with the Field
    // Name, Start Index and End Index

    public List<String> getFileLayout(String layoutFileName) throws Exception {

        List<String> fileLayoutList = new ArrayList<String>();
        File layoutFile = new File(layoutFileName);

        FileInputStream layoutFileInputStream = new FileInputStream(layoutFile);
        BufferedReader layoutBuffReader = new BufferedReader(
                new InputStreamReader(layoutFileInputStream));
        String currentLine;
        try {
            while ((currentLine = layoutBuffReader.readLine()) != null) {
                if ((currentLine.trim().equals(""))) {
                    throw new Exception(
                            "There should not be any empty lines in the middle of the Layout File");
                }

                String fieldName = currentLine.substring(0,
                        currentLine.indexOf(":"));
                String startIndex = currentLine.substring(
                        currentLine.indexOf(":") + 2, currentLine.indexOf(","));
                String endIndex = currentLine.substring(
                        currentLine.indexOf(",") + 1,
                        currentLine.lastIndexOf(")"));
                fileLayoutList.add(fieldName);
                fileLayoutList.add(startIndex);
                fileLayoutList.add(endIndex);
                // System.out.println(fieldName);
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            throw new Exception(
                    "You have not provided the Layout File for processing. Please provide it and try again");
        }

        return fileLayoutList;
    }

    // Get the Actual and Expected File and compare according to the position

    public void compareExpectedActual(String actualFileName,
            String expectedFileName, List<String> fileLayoutList)
            throws Exception {

        File actualFile = new File(actualFileName);
        File expectedFile = new File(expectedFileName);
        FileInputStream actualFileInputStream = new FileInputStream(actualFile);
        BufferedReader actBuffReader = new BufferedReader(
                new InputStreamReader(actualFileInputStream));

        FileInputStream expectedFileInputStream = new FileInputStream(
                expectedFile);
        BufferedReader expBuffReader = new BufferedReader(
                new InputStreamReader(expectedFileInputStream));
        HSSFWorkbook excelWorkbook = new HSSFWorkbook();
        HSSFSheet excelSheet = excelWorkbook.createSheet("File Comparator");
        HSSFRow headerExcelRow = excelSheet.createRow(1);
        HSSFRow currExcelRow = null;
        HSSFCell headerExcelCell = null;
        HSSFCell currExcelCell = null;

        headerExcelCell = headerExcelRow.createCell(1);
        headerExcelCell.setCellValue("Field Name");
        for (int fieldName = 2, listNum = 0; listNum < fileLayoutList.size(); fieldName++) {
            currExcelRow = excelSheet.createRow(fieldName);
            currExcelCell = currExcelRow.createCell(1);
            // System.out.println(listNum);
            currExcelCell.setCellValue(fileLayoutList.get(listNum));
            listNum += 3;
        }
        System.out.println(fileLayoutList.size());
        int excelNum = 2;
        for (String actualFileCurrLine, expectedFileCurrLine; (actualFileCurrLine = actBuffReader
                .readLine()) != null
                && (expectedFileCurrLine = expBuffReader.readLine()) != null; excelNum += 4) {
            char[] actualArray = actualFileCurrLine.toCharArray();
            char[] expectedArray = expectedFileCurrLine.toCharArray();
            for (int i = 0, excelRow = 2; i < fileLayoutList.size(); i += 3, excelRow++) {
                boolean resultOfCompare = false;
                String expectedString = "";
                String actualString = "";
                for (int j = Integer.parseInt(fileLayoutList.get(i + 1)); j <= Integer
                        .parseInt(fileLayoutList.get(i + 2)); j++) {

                    expectedString += expectedArray[j - 1];
                    // System.out.println("Array Index"+j);
                    System.out.println(fileLayoutList.get(i + 1));
                    System.out.println(fileLayoutList.get(i + 2));
                    actualString += actualArray[j - 1];

                }
                if (expectedString.equals(actualString))
                    resultOfCompare = true;
                System.out.println(expectedString + "-" + actualString);
                System.out.println("Row Number" + excelRow);
                headerExcelCell = headerExcelRow.createCell(excelNum);
                headerExcelCell.setCellValue("Actual");
                headerExcelCell = headerExcelRow.createCell(excelNum + 1);
                headerExcelCell.setCellValue("Expected");
                headerExcelCell = headerExcelRow.createCell(excelNum + 2);
                headerExcelCell.setCellValue("Result");
                System.out.println("Cell Value" + "[" + excelRow + ","
                        + excelNum + "]=" + actualString);
                currExcelRow = excelSheet.getRow(excelRow);
                currExcelCell = currExcelRow.createCell(excelNum);
                currExcelCell.setCellValue(actualString);
                System.out.println("Cell Value" + "[" + excelRow + ","
                        + (excelNum + 1) + "]=" + actualString);
                currExcelCell = currExcelRow.createCell(excelNum + 1);
                currExcelCell.setCellValue(expectedString);
                System.out.println("Cell Value" + "[" + excelRow + ","
                        + (excelNum + 2) + "]=" + resultOfCompare);
                currExcelCell = currExcelRow.createCell(excelNum + 2);
                currExcelCell.setCellValue(resultOfCompare);

            }

        }

        FileOutputStream s = new FileOutputStream("FlatfileComparator.xls");
        excelWorkbook.write(s);
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9835426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档