트리 선택 수신기를 사용하여 선택한 폴더의 각 콘텐츠와 파일을 비교하고 싶습니다. 다음과 같이 트리 선택 수신기 코드가 있습니다.트리에서 폴더를 선택했을 때 폴더 또는 하드 드라이브 내에서 동일한 파일 찾기
//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;
}
}
하지만 난이 예외를 받고 있어요 :
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);
}
}
});
재귀 방법은 예외 caught.java.lang.NullPointerException
어떤 도움을 바랍니다.
스택 추적주세요. –
미안합니다. 스택 추적이란 무엇입니까? –
정확한 오류는 무엇입니까? 이 [스택 추적을 인쇄 할 수 있습니까] (http://stackoverflow.com/a/6823007/1492578) 예외가 있습니까? –