Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 批处理文件以比较具有不同数据的两个不同文件_File_Compare - Fatal编程技术网

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

File 批处理文件以比较具有不同数据的两个不同文件,file,compare,File,Compare,我想比较两个文件,如果数据不匹配,则打印消息“数据不相同”,如果匹配成功,则打印“数据相同” 第一个文件的内容(Live.txt): 内容第二个文件(Sup.txt): 操作系统:Microsoft Windows上的Windows7 在Linux和类似系统上 cmp <file1> <file2> cmp 将告诉您文件是否不同,以及: diff <file1> <file2> diff 将显示差异。我们还可以使用特定布局逐字节编写大型文件

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

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

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

操作系统:Microsoft Windows上的Windows7

在Linux和类似系统上

cmp <file1> <file2>
cmp
将告诉您文件是否不同,以及:

diff <file1> <file2>
diff

将显示差异。

我们还可以使用特定布局逐字节编写大型文件,并在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);
    }

}
导入java.awt.image.SampleModel;
导入java.io.BufferedReader;
导入java.io.Closeable;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
导入java.util.StringTokenizer;
导入org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
公共类FlatFileComparator{
/*
*拿三个平面锉刀。
* 
*一个用于布局,第二个用于预期文件,第三个用于实际文件
*/
公共静态void main(字符串args[])引发异常{
字符串fileName=“recordLayout.txt”;
字符串actualFileName=“Actual.txt”;
字符串expectedFileName=“Expected.txt”;
List-recordLayout=null;
FlatFileComparator fb=新的FlatFileComparator();
recordLayout=fb.getFileLayout(文件名);
fb.compareExpectedActual(实际文件名、预期文件名、记录布局);
}
//获取布局的记录名称,并将其与字段一起放入列表中
//名称、开始索引和结束索引
公共列表getFileLayout(字符串layoutFileName)引发异常{
List fileLayoutList=newarraylist();
文件layoutFile=新文件(layoutFileName);
FileInputStream layoutFileInputStream=新的FileInputStream(layoutFile);
BufferedReader layoutBuffReader=新的BufferedReader(
新的InputStreamReader(layoutFileInputStream));
串电流线;
试一试{
而((currentLine=layoutBuffReader.readLine())!=null){
if((currentLine.trim()等于(“”)){
抛出新异常(
“在布局文件的中间不应该有空行”;
}
String fieldName=currentLine.substring(0,
currentLine.indexOf(“:”);
字符串startIndex=currentLine.substring(
currentLine.indexOf(“:”)+2,currentLine.indexOf(“,”);
字符串endIndex=currentLine.substring(
currentLine.indexOf(“,”)+1,
currentLine.lastIndexOf(“)”);
fileLayoutList.add(字段名);
fileLayoutList.add(startIndex);
fileLayoutList.add(endIndex);
//System.out.println(字段名);
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
抛出新异常(
“您尚未提供要处理的布局文件。请提供并重试”);
}
返回文件布局列表;
}
//获取实际文件和预期文件,并根据位置进行比较
public void compareeExpectedActual(字符串actualFileName,
字符串expectedFileName,列表文件布局列表)
抛出异常{
文件实际文件=新文件(实际文件名);
File expectedFile=新文件(expectedFileName);
FileInputStream actualFileInputStream=新的FileInputStream(actualFile);
BufferedReader actBuffReader=新的BufferedReader(
新的InputStreamReader(actualFileInputStream));
FileInputStream expectedFileInputStream=新FileInputStream(
预期文件);
BufferedReader expBuffReader=新的BufferedReader(
新的InputStreamReader(expectedFileInputStream));
HSSFWorkbook excelWorkbook=新的HSSFWorkbook();
HSSFSheet excelSheet=excelWorkbook.createSheet(“文件比较器”);
HSSFRow headerExcelRow=excelSheet.createRow(1);
HSSFRow currow=null;
HSSFCell headerExcelCell=null;
HSSFCell currExcelCell=null;
headerExcelCell=headerExcelRow.createCell(1);
headerExcelCell.setCellValue(“字段名”);
对于(int fieldName=2,listNum=0;listNum对于(int j=Integer.parseInt(fileLayoutList.get(i+1));j您尝试过自己搜索吗?:)不,我不知道批处理编程,所以,使用谷歌来了解一些想法吧!
diff <file1> <file2>
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);
    }

}