문제

I'm working on a project where I need to read in two files, and then compare them in order to find how many lines are different. For example, if I had two files: 1.Cat 2.Dog 3.Shark 4.Penguin and 1.Cat 2.Dog 3.Penguin 4.Octopus The end result would say, "These files are not the same, there are two differences." Here is my coding so far: I have read the files in and stored each line of the file in a new part of the list. Now I am just looking for a way to compare them properly in order to find there differences. The way i am comparing them now does not account for order :/ Here is my code:

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


    }
}
도움이 되었습니까?

해결책

You could iterate over these two files line by line using a BufferedReader

the readLine() method returns a string representation of each line which can then be checked for equality.

The index of any unequal lines can be stored and returned in any way you want.

다른 팁

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);
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top