我收到了FileNotFoundException。在其他地方使用的相同功能工作正常。请告诉我这段代码有什么问题:
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());
}
if
块工作正常,但else if
块正在提供此异常:
/Users/esna786/File2.txt
java.io.FileNotFoundException: File2.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at java.io.FileReader.<init>(FileReader.java:58)
at compareTwoTextualFiles.compareFiles(compareTwoTextualFiles.java:27)
at comparisonForm$1.valueChanged(comparisonForm.java:58)
at javax.swing.JTree.fireValueChanged(JTree.java:2926)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:3387)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:635)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1093)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultTreeSelectionModel.java:294)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(DefaultTreeSelectionModel.java:188)
at javax.swing.JTree.setSelectionPath(JTree.java:1633)
at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.java:2393)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3609)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3548)
at java.awt.Component.processMouseEvent(Component.java:6522)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4530)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
else if
部分正在调用此代码块:
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;
请帮我解决这个问题。
答案 0 :(得分:1)
尝试打印path。检查这是否是您希望文件所在的位置。
答案 1 :(得分:1)
我最终通过将上面的代码替换为:
来解决它try {
// Create FileReader & Writer Objects.
FileReader File1Reader = new FileReader(File1.getAbsolutePath());
FileReader File2Reader = new FileReader(File2.getAbsolutePath());