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

时间:2012-03-23 07:21:05

标签: file compare

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

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

Last
4000
5000
(2 Row affected)

内容第二档(Sup.txt):

Last
3000
6000
(2 Row affected)

操作系统:Windows7

2 个答案:

答案 0 :(得分:1)

在Microsoft Windows you can use fc command上。

在Linux和类似系统上

cmp <file1> <file2>

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

diff <file1> <file2>

将显示差异。

答案 1 :(得分:0)

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

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);
    }

}