比较java中的两个文件
我试图比较两个.txt文件(即它们的内容),但是当我执行这个代码时,我的应用程序进入无限循环。 为什么?
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; }
我将您的代码转换为主程序。 此代码中没有无限循环。
我假设您正在比较两个小尺寸的文本文件。
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); } }
我在2个不同的小文本文件上运行它。 这是输出。
javac Diff.java && java Diff a.txt b.txt Flag 0
如果您认为自己有无限循环,则问题可能出在其他地方。
代码看起来不错,没有无限循环。 您可以删除代码中的无关检查,并可以更新代码,如下所示:
int flag=1; while (((line1 = reader1.readLine()) != null)&&((line2 = reader2.readLine()) != null)) { if (!line1.equalsIgnoreCase(line2)) { flag=0; break; } }
由于方法的返回类型是整数,如果不同则返回0
,如果相等则返回1
。
假设文本文件输入, while
循环的替代实现:
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 }
第一个else if
没有必要,但为了清楚起见包括在内。 否则,上面的代码可以简化为:
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; } }
循环应置于try/finally
块中,以确保读取器已关闭。
如果file2与file1相同但在末尾有一个额外的行,则Jess的上述方法将失败。
这应该工作。
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 list1 = new ArrayList (); List list2 = new ArrayList (); while ((thisLine = br1.readLine()) != null) { list1.add(thisLine); } while ((thatLine = br2.readLine()) != null) { list2.add(thatLine); } br1.close(); br2.close(); return list1.equals(list2); }
如果你使用java8,下面的代码来比较文件内容
public boolean compareTwoFiles(String file1Path, String file2Path){ Path p1 = Paths.get(file1Path); Path p1 = Paths.get(file1Path); try{ List listF1 = Files.readAllLines(p1); List listF2 = Files.readAllLines(p2); return listF1.containsAll(listF2); }catch(IOException ie) { ie.getMessage(); } }