簡體   English   中英

在樹上選擇文件夾時,在文件夾或硬盤驅動器中查找相同的文件

[英]Find identical files within folder or hard drives when folder selection is made on tree

我希望將文件與使用樹選擇偵聽器選擇的文件夾的每個內容進行比較。我有一個樹選擇偵聽器代碼,如下所示。

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

但是我正在收到此異常:捕獲到的異常.java.lang.NullPointerException

請幫忙。

方法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並報告。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM