从目录中读取文件并在 java 中的嵌套 for 循环中比较文件。第一个文件比较,但其余的说“不一样”,即使它是相同的。我知道我必须在循环中调整一些东西以防止它发送空值、任何指针
File Directory = new File (location);
File files[] = Directory.listFiles();
for (File f : files)
{
for (File g : files)
{
br = new BufferedReader (new FileReader (f));
while(( z = br.readLine()) != null) s1+= z;
br2 = new BufferedReader (new FileReader (g));
while ((y = br2.readLine()) != null) s2 += y;
System.out.println();
// System.out.println(s1);
// System.out.println(f.getName() + "=" + g.getName());
if (s1.equals(s2)) {
System.out.println(f.getName() + "=" + g.getName());
System.out.println( "Content of both files are same");
}
else {
System.out.println(f.getName() + "!=" + g.getName());
System.out.println("Content of both files are not same");
}
}