简体   繁体   English

比较两个文件并用Java进行总结

[英]Compare two file and summarize with Java

I have 2 txt file and both have some different data. 我有2个txt文件,并且都有一些不同的数据。 And I am comparing both then summarize report for where and change has done. 我正在比较两者,然后总结报告在哪里进行了更改。 But my code is running for only 1 line for both file: 但是我的代码对于这两个文件仅运行了1行:

Input 输入项

v1.txt:

  ABCD DEFG
  XYZ AAA
  NNN

v2.txt:

  ABCD DEF
  XYZ AAA
  NN

Output what i have received: Two files have different content. 输出我收到的内容:两个文件具有不同的内容。

They differ at line 1
File1 has ABCD DEFG and File2 has ABCD DEF at line 1

Code: 码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class CompareTextFiles {
    public static void main(String[] args) throws IOException {
        BufferedReader reader1 = new BufferedReader(new FileReader("Data\v1.txt"));
        BufferedReader reader2 = new BufferedReader(new FileReader("Data\v2.txt"));
        String line1 = reader1.readLine();
        String line2 = reader2.readLine();
        boolean areEqual = true;
        int lineNum = 1;
        while (line1 != null || line2 != null) {
            if (line1 == null || line2 == null) {
                areEqual = false;
                break;
            } else if (!line1.equalsIgnoreCase(line2)) {
                areEqual = false;
                break;
            }
            line1 = reader1.readLine();
            line2 = reader2.readLine();
            lineNum++;
        }
        if (areEqual) {
            System.out.println("Two files have same content.");
        } else {
            System.out.println("Two files have different content. They differ at line " + lineNum);
            System.out.println("File1 has " + line1 + " and File2 has " + line2 + " at line " + lineNum);
        }
        reader1.close();
        reader2.close();
    }
}

Here is the code that has been revised and formatted. 这是已修改和格式化的代码。

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 

public class CompareTextFiles {
    public static void main(String[] args) throws IOException { 
        BufferedReader reader1 = new BufferedReader(new FileReader("Data\v1.txt")); 
        BufferedReader reader2 = new BufferedReader(new FileReader("Data\v2.txt")); 
        String line1 = reader1.readLine(); 
        String line2 = reader2.readLine(); 
        boolean areEqual = true; 
        int lineNum = 1; 
        while (line1 != null || line2 != null) { 
            if (line1 == null || line2 == null) { 
                areEqual = false; 
                break; 
            } else if (!line1.equalsIgnoreCase(line2)) { 
                System.out.println("Two files have different content at line " + lineNum); 
                System.out.println("File1 has " + line1 + " and File2 has " + line2 + " at line " + lineNum); 

            } 
            line1 = reader1.readLine(); 
            line2 = reader2.readLine(); 
            lineNum++; 
        } 

        reader1.close(); 
        reader2.close(); 
    } 
}

The changes I have done: 我所做的更改:

  1. Moved the if condition from after the loop to inside the loop so it prints out each line that is different. 将if条件从循环之后移到循环内部,以便打印出不同的每一行。
  2. Changed the wording of the System.out. 更改了System.out的措辞。
  3. Removed the break; 删除中断; from the if condition where the lines are not equal. 从if条件,其中行不相等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM