简体   繁体   中英

Comparing two files in java

I am trying to compare two .txt files (ie their contents), but when I execute this code my application goes into an infinite loop. Why?

public int compareFile(String fILE_ONE2, String fILE_TWO2)throws Exception 
{

File f1 = new File(fILE_ONE2); //OUTFILE
File f2 = new File(fILE_TWO2); //INPUT

FileReader fR1 = new FileReader(f1);
FileReader fR2 = new FileReader(f2);

BufferedReader reader1 = new BufferedReader(fR1);
BufferedReader reader2 = new BufferedReader(fR2);

String line1 = null;
String line2 = null;
int flag=1;
while ((flag==1) &&((line1 = reader1.readLine()) != null)&&((line2 = reader2.readLine()) != null)) 
{
    if (!line1.equalsIgnoreCase(line2))  
        flag=0;
    else 
        flag=1;   
}
reader1.close();
reader2.close();
return flag;


}

I converted your code into a main program. There is no infinite loop in this code.

I am assuming you are comparing 2 text files of a small-ish size.

import java.io.*;

public class Diff {
    public static void main(String[] args) throws FileNotFoundException, IOException {

        File f1 = new File(args[0]);// OUTFILE
        File f2 = new File(args[1]);// INPUT

        FileReader fR1 = new FileReader(f1);
        FileReader fR2 = new FileReader(f2);

        BufferedReader reader1 = new BufferedReader(fR1);
        BufferedReader reader2 = new BufferedReader(fR2);

        String line1 = null;
        String line2 = null;
        int flag = 1;
        while ((flag == 1) && ((line1 = reader1.readLine()) != null)
                && ((line2 = reader2.readLine()) != null)) {
            if (!line1.equalsIgnoreCase(line2))
                flag = 0;
        }
        reader1.close();
        reader2.close();
        System.out.println("Flag " + flag);
    }
}

I ran it on 2 small different text files. This is the output.

javac Diff.java && java Diff a.txt b.txt
Flag 0

If you think you have an infinite loop, the issue might be elsewhere.

The code looks good, no infinite loops. You can remove irrespective check in the code and can update the code as below:

int flag=1;
while (((line1 = reader1.readLine()) != null)&&((line2 = reader2.readLine()) != null)) 
{
    if (!line1.equalsIgnoreCase(line2)) 
    { 
        flag=0; 
        break;
    }
}

As the return type of the method is integer than it will return 0 if different and 1 if equal.

Assuming text file inputs, an alternative implementation to the while loop:

while (true) // Continue while there are equal lines
{
    line1 = reader1.readLine();
    line2 = reader2.readLine();

    if (line1 == null) // End of file 1
    {
        return (line2 == null ? 1 : 0); // Equal only if file 2 also ended
    }
    else if (line2 == null)
    {
        return 0; // File 2 ended before file 1, so not equal 
    }
    else if (!line1.equalsIgnoreCase(line2)) // Non-null and different lines
    {
        return 0;
    }

    // Non-null and equal lines, continue until the input is exhausted
}

The first else if is not necessary, but it is included for clarity purposes. Otherwise, the above code could be simplified to:

while (true) // Continue while there are equal lines
{
    line1 = reader1.readLine();
    line2 = reader2.readLine();

    if (line1 == null) // End of file 1
    {
        return (line2 == null ? 1 : 0); // Equal only if file 2 also ended
    }

    if (!line1.equalsIgnoreCase(line2)) // Different lines, or end of file 2
    {
        return 0;
    }
}

The loop should be placed in a try/finally block, to assure that the readers are closed.

Above method by Jess will fail if file2 is same as file1 but has an extra line at the end.

This should work.

public boolean compareTwoFiles(String file1Path, String file2Path)
            throws IOException {

    File file1 = new File(file1Path);
    File file2 = new File(file2Path);

    BufferedReader br1 = new BufferedReader(new FileReader(file1));
    BufferedReader br2 = new BufferedReader(new FileReader(file2));

    String thisLine = null;
    String thatLine = null;

    List<String> list1 = new ArrayList<String>();
    List<String> list2 = new ArrayList<String>();

    while ((thisLine = br1.readLine()) != null) {
        list1.add(thisLine);
    }
    while ((thatLine = br2.readLine()) != null) {
        list2.add(thatLine);
    }

    br1.close();
    br2.close();

    return list1.equals(list2);
}

if you use java8, the code below to compare file contents

public boolean compareTwoFiles(String file1Path, String file2Path){
      Path p1 = Paths.get(file1Path);
      Path p1 = Paths.get(file1Path);

try{
        List<String> listF1 = Files.readAllLines(p1);
    List<String> listF2 = Files.readAllLines(p2);
    return listF1.containsAll(listF2);

        }catch(IOException ie) {
            ie.getMessage();
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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