在树上进行文件夹选择时,在文件夹或硬盘驱动器中查找相同的文件

时间:2014-12-26 18:49:16

标签: java file bufferedreader filereader

我希望将文件与使用树选择侦听器选择的文件夹的每个内容进行比较。我有一个树选择侦听器代码,如下所示。

 public void valueChanged(TreeSelectionEvent event) {
            redFolder = (File) redfileTree.getLastSelectedPathComponent();
            Functions obj = new Functions();

            try {
                if (redFolder.isDirectory()) {
                    matchedfileTextArea.setText(obj.compare_With_TreeFolder(redFile, redFolder).toString());
                }
            } catch (Exception e) {
                System.out.println("Exception caught." + e);
            }
        }
    });

递归方法是:

//compare the selected file with the selected folder in the tree
public File compare_With_TreeFolder(File redFile, File redFolder) throws IOException, Exception {
    String[] folderContents = redFolder.list();
    File file_Folder;
    for (String str : folderContents) {
        file_Folder = new File(str);
        if (file_Folder.isDirectory()) {
            compare_With_TreeFolder(redFile, file_Folder);
        } else if (file_Folder.isFile()) {
            if (redFile.getName().equals(file_Folder.getName())) {
                System.out.println(redFile.getName() + file_Folder.getName());
                if (redFile.length() == file_Folder.length()) {
                    if (compareFile(redFile, file_Folder) == 1) {
                        return file_Folder;
                    }
                }
            }
        }
    }
    return null;
}

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

    File f1 = new File(fILE_ONE2.toString()); //OUTFILE
    File f2 = new File(fILE_TWO2.toString()); //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 (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;
        }
    }

但是我得到了这个例外:      异常catch.java.lang.NullPointerException

请帮助。

1 个答案:

答案 0 :(得分:0)

方法compare_With_TreeFolder可能返回null。然后,以下行将尝试在null上调用方法toString,这是NPE的一个很好的理由。

matchedfileTextArea.setText(obj.compare_With_TreeFolder(redFile, redFolder).toString());

我不确定返回值null应该指示什么,但必须在使用toString之前检查它。

<强>后来

进一步改进 - 请参阅XXX

System.out.println("Exception caught." + e);
e.printStackTrace();   // XXX Add this to learn about the location of an Exception

public File compare_With_TreeFolder(File redFile, File redFolder)
  throws IOException, Exception {
  String[] folderContents = redFolder.list();
  File file_Folder;
  for (String str : folderContents) {
    file_Folder = new File(redFolder, str);    // XXX prefix the file with the folder!
    if (file_Folder.isDirectory()) {
      // XXX if the recursive call finds a match: return the File
      File res = compare_With_TreeFolder(redFile, file_Folder);
      if( res != null ) return res;
    } else if (file_Folder.isFile()) {
      if (redFile.getName().equals(file_Folder.getName())) {
        if (redFile.length() == file_Folder.length()) {
          if (compareFile(redFile, file_Folder) == 1) {
            return file_Folder;
          }
        }
      }
    }
  }
  return null;
}

我还没有找到NPE的任何其他原因。我建议你添加printStackTrace并报告回来。