比较两个文件并确定哪些行不同

时间:2014-04-22 19:18:26

标签: java string arraylist java.util.scanner

我正在处理一个项目,我需要读取两个文件,然后比较它们以找出不同的行数。例如,如果我有两个文件: 1.Cat 2.Dog 3.Shark 4.Penguin     和 1.Cat 2.Dog 3.Penguin 4.Octopus 最终的结果会说,"这些文件不一样,有两点不同。" 这是我到目前为止的编码:我已经读取了文件并将其存储在列表的新部分中。现在我只是想找到一种方法来正确地比较它们以找到差异。我现在比较它们的方式不考虑顺序:/这是我的代码:

import java.io.*;
import java.util.*;

public class myfilereader
{
    public static void main (String[] args) throws java.io.IOException
    {


        ArrayList<String> ArrayList1 = new ArrayList<String>();
        ArrayList<String> ArrayList2 = new ArrayList<String>();
        ArrayList<String> ArrayList3 = new ArrayList<String>();
        try
        {
            Scanner File1 = new Scanner(new File("/Users/Home/Desktop/File1.txt"));
            while (File1.hasNext())
            {
                ArrayList1.add(File1.next());
            }

            Scanner File2 = new Scanner(new File("/Users/Home/Desktop/File2.txt"));
            while (File2.hasNextLine())
            {
                ArrayList2.add(File2.next());
            }
        }
        catch (FileNotFoundException ex)
        {
            ex.printStackTrace();
        }
        for (String ArrayList : ArrayList1) 
        {
            System.out.println("File 1: " + ArrayList1);
        }
        for (String ArrayList : ArrayList2) 
        {
            System.out.println("File 2: " + ArrayList2);
        }
        ArrayList1.removeAll(ArrayList2);
        System.out.println(ArrayList1);


    }
}

2 个答案:

答案 0 :(得分:2)

您可以使用BufferedReader

逐行迭代这两个文件

readLine()方法返回每行的字符串表示,然后可以检查它是否相等。

任何不相等的行的索引都可以以任何你想要的方式存储和返回。

答案 1 :(得分:-1)

void compareFiles() {
    BufferedReader bfr1 = null;
    BufferedReader bfr2 = null;
    List<String> file1Data = new ArrayList<>();
    List<String> file2Data = new ArrayList<>();
    List<String> matchedLines = new ArrayList<>();
    List<String> unmatchedLines = new ArrayList<>();

    try {
        File file1 = new File("F:/file1.txt");
        File file2 = new File("F:/file2.txt");
        bfr1 = new BufferedReader(new FileReader(file1));
        bfr2 = new BufferedReader(new FileReader(file2));

        String name1;
        String name2;
        try {
            // read the first file and store it in an ArrayList
            while ((name1 = bfr1.readLine()) != null) {
                file1Data.add(name1);
            }
            // read the second file and store it in an ArrayList
            while ((name2 = bfr2.readLine()) != null) {
                file2Data.add(name2);
            }

            // iterate once over the first ArrayList and compare with the second.
            for (String key1 : file1Data) {
                if (file2Data.contains(key1)) { // here is your desired match
                    matchedLines.add(key1);
                } else {
                    unmatchedLines.add(key1);
                }
            }
            System.out
                    .println("Matched Lines are: \n" + matchedLines + "\nUnmatched Lines are: \n" + unmatchedLines);

        } catch (Exception e) {
            e.printStackTrace();
        }

    } catch (FileNotFoundException ex) {
        System.out.println(ex);
    } finally {
        try {
            bfr1.close();
            bfr2.close();
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}