2012-03-23 115 views
0

我想比較兩個文件,如果數據不匹配,然後打印一條消息「數據不相同」如果匹配成功,則打印「數據是一樣的「。第一個文件(Live.txt)的批處理文件比較具有不同數據的兩個不同文件

內容:

Last 
4000 
5000 
(2 Row affected) 

內容第二個文件(Sup.txt):

Last 
3000 
6000 
(2 Row affected) 

操作系統:Windows7的

+1

哪個操作系統的差異,或殼呢? – 2012-03-23 08:59:47

+0

您是否嘗試過自行搜索? :) – 2012-03-23 09:00:43

+0

不,我不知道批量編程 – skcwebworld 2012-03-26 09:57:11

回答

1

在Microsoft Windows you can use fc command

在Linux和類似系統

cmp <file1> <file2> 

會告訴你,如果這些文件是不同的:

diff <file1> <file2> 

將顯示差異。

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

} 
相關問題