Java I';我得到了FileNotFoundException
我得到了FileNotFoundException。在其他地方使用的相同功能工作正常。请告诉我此代码有什么问题:Java I';我得到了FileNotFoundException,java,swing,jtree,filenotfoundexception,Java,Swing,Jtree,Filenotfoundexception,我得到了FileNotFoundException。在其他地方使用的相同功能工作正常。请告诉我此代码有什么问题: try { if (redFolder.isDirectory() && redFile.isFile()) { Functions.matched_file_names=new ArrayList<>(); obj.compare_With_TreeFolder(redFile, redFolder);
try {
if (redFolder.isDirectory() && redFile.isFile()) {
Functions.matched_file_names=new ArrayList<>();
obj.compare_With_TreeFolder(redFile, redFolder);
StringBuffer bfr=new StringBuffer();
for(String item:Functions.matched_file_names)
bfr.append(item+"\n");
matchedfileTextArea.setText(bfr.toString());
} else if(redFile.isFile() && redFolder.isFile()){
compareTwoTextualFiles cttf=new compareTwoTextualFiles();
matchedfileTextArea.setText(cttf.compareFiles(redFile, redFolder));
}
} catch (NullPointerException e) {
JOptionPane.showMessageDialog(null, "Please Select File First.");
} catch (Exception e) {
System.out.println("ExceptionCaught. "+e.getMessage());
}
else如果
部分调用此代码块:
try {
// Create FileReader & Writer Objects.
FileReader File1Reader = new FileReader(File1.toString());
FileReader File2Reader = new FileReader(File2.toString());
// Create Buffered Object.
BufferedReader File1BufRdr = new BufferedReader(File1Reader);
BufferedReader File2BufRdr = new BufferedReader(File2Reader);
// Get the file contents into String Variables.
String File1Content = File1BufRdr.readLine();
String File2Content = File2BufRdr.readLine();
//New String Builder
StringBuilder buffer = new StringBuilder();
// Compare the Contents of the files.
String startOfComparision = "---------START----------";
buffer.append(startOfComparision).append("\n");
boolean isDifferent = false;
int lineNumber = 1;
if (File1Content != null || File2Content != null) {
// Check whether file1 contains data or not
while ((File1Content != null)) {
// Check whether file2 contains data or not
if (((File2Content) != null)) {
// Check whether both the files have same data in the lines
if (!File1Content.equals(File2Content)) {
buffer.append("Difference in Line " + lineNumber + " :- " + File1.getName() + " contains :" + File1Content + " " + File2.getName() + " Contains : " + File2Content).append("\n");
isDifferent = true;
}
lineNumber = lineNumber + 1;
File2Content = File2BufRdr.readLine();
} else {
buffer.append("Difference in Line " + lineNumber + " :- " + File1.getName() + " contains :" + File1Content + " " + File2.getName() + " Contains - " + File2Content).append("\n");
isDifferent = true;
lineNumber = lineNumber + 1;
}
File1Content = File1BufRdr.readLine();
}
// Check for the condition : if File2 has Data & File1 doesn't contain data.
while ((File2Content != null) && (File1Content == null)) {
buffer.append("Difference in Line " + lineNumber + " :- " + File1.getName() + " contains :" + File1Content + " " + File2.getName() + " Contains - " + File2Content).append("\n");
isDifferent = true;
lineNumber = lineNumber + 1;
File2Content = File2BufRdr.readLine();
}
} else {
// Mention that both the files don't have Data.
buffer.append(File1.getName() + " and " + File2.getName() + " do not contain any data.");
isDifferent = true;
}
// Check is there any difference or not.
String endOfComparision = "-----------END----------";
if (isDifferent) {
buffer.append(endOfComparision).append("\n");
} else {
buffer.append("No Difference Found \nThe Contents Of The Files Are Identical.").append("\n");
buffer.append(endOfComparision).append("\n");
Functions.matched_file_names.add("Path: " + File2.getAbsolutePath() + "\nFile Name: " + File2.getName());
}
//Close the streams.
File1Reader.close();
File2Reader.close();
File1BufRdr.close();
File2BufRdr.close();
float percentage = (float) (getCommonWords(File1, File2) / get_Total_Number_Of_Words(File1)) * 100;
return buffer.toString() + " \n\nThe Total number of common words of " + File1.getName() + " and " + File2.getName() + " are: " + getCommonWords(File1, File2) + "\n\nThe " + File1.getName() + " is " + Math.ceil(percentage) + " % matched with " + File2.getName() + ".";
} catch (FileNotFoundException e) {
e.printStackTrace();
//JOptionPane.showMessageDialog(null, "Please Select Files." + e.getMessage());
}
return null;
请帮助我解决此问题。请尝试打印。检查您是否希望该文件位于该位置。我最终通过替换上述代码解决了该问题:
try {
// Create FileReader & Writer Objects.
FileReader File1Reader = new FileReader(File1.getAbsolutePath());
FileReader File2Reader = new FileReader(File2.getAbsolutePath());
这很难理解。另外,您是否仔细检查了路径是否存在?文件存在吗?拼写是否正确?是的,我使用filename.getabsolutepath.chceck检查了它是否存在路径或文件,因为它的简单文件NotFoundException.put comment not answer:)我尝试使用filename.getabsolutepath打印路径。它仍然不工作try(exists)[。如果它返回false,那么肯定没有文件或等效文件(例如:文件/目录权限)。这很简单,因为错误只清楚地说明了文件名,您需要文件的完整路径,而不是名称。很高兴您自己解决了这个问题,这样您就再也不会卡在这里了。
try {
// Create FileReader & Writer Objects.
FileReader File1Reader = new FileReader(File1.getAbsolutePath());
FileReader File2Reader = new FileReader(File2.getAbsolutePath());