FileNotFoundException: 但文件确实存在
FileNotFoundException: But the file does exist
也许,这个问题在这里已经被问过很多次了,但是请原谅我,因为我花了几个小时也无法解决它。我正在尝试从我的目录加载一个文件并将其显示在 TextArea 上。但是,我一直得到java.io.FileNotFoundException。一开始我以为是文件权限的问题,但是我改了权限之后,还是报同样的错误。我检查了很多次路径是否正确或拼写是否正确。我什至尝试将错误堆栈跟踪的路径粘贴到我的终端,并且该路径正在运行。这是我的代码:
private void treeFileValueChanged(javax.swing.event.TreeSelectionEvent evt) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) treeFile.getLastSelectedPathComponent();
if(selectedNode==null)
JOptionPane.showMessageDialog(this, "Error");
if(selectedNode.isLeaf()){
String path = Arrays.toString(selectedNode.getUserObjectPath());
path = path.replaceAll("[\[\]\:,]","");
String[] _path = path.split(" ");
String filePath="";
int counter=1;
for (String s : _path) { // I tried to re-construct the path of the selected node/child
if(counter==_path.length){
filePath += s;
counter = 1;
}else{
filePath += s+"/";
counter++;
}
}
try {
setTextArea(filePath); //passing the filePath in string format
} catch (FileNotFoundException ex) {
Logger.getLogger(Preprocess.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Preprocess.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void setTextArea(String _filePath) throws FileNotFoundException, IOException{
File file = new File(_filePath);
if(!file.exists())
System.out.println("File not found");
String dir = System.getProperty("user.dir");
System.out.println("Current sys dir: " + dir);
System.out.println("Current abs dir: "+file.getAbsolutePath());
BufferedReader br = new BufferedReader(new FileReader(file.getName()));
try{
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null){
sb.append(line);
sb.append(System.lineSeparator());
}
textPreprocess.setText(sb.toString());
}finally{
br.close();
}
}
下面是输出:
Current sys dir: /Users/adibangun/Downloads/ThematicAnalysis
Current abs dir: /Users/adibangun/Downloads/ThematicAnalysis/Output/Sentiment/Sentiment20150629.txt
Jun 29, 2015 11:32:55 PM thematicanalysis.GUI.Preprocess treeFileValueChanged
SEVERE: null
java.io.FileNotFoundException: Sentiment20150629.txt (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
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 thematicanalysis.GUI.Preprocess.setTextArea(Preprocess.java:197)
at thematicanalysis.GUI.Preprocess.treeFileValueChanged(Preprocess.java:163)
at thematicanalysis.GUI.Preprocess.access[=11=]0(Preprocess.java:33)
at thematicanalysis.GUI.Preprocess.valueChanged(Preprocess.java:75)
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:3324)
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:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:731)
at java.awt.EventQueue.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
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)
编辑:
密码文件:/Users/adibangun/Downloads/ThematicAnalysis/Output/Sentiment
ls-ltr Sentiment20150629.txt : -rw-r--r-- 1 adibangun 员工 408555 6 月 29 日 23:13 Sentiment20150629.txt
有人知道这个问题吗?任何评论和帮助将不胜感激。非常感谢
将BufferedReader br = new BufferedReader(new FileReader(file.getName()));
替换为BufferedReader br = new BufferedReader(new FileReader(file));
在以下情况下抛出 FileNotFoundException:
- 如果异常消息提示没有这样的文件或目录,那么您必须验证指定的是否正确并且实际指向您系统中存在的文件或目录。
- 如果异常消息提示权限被拒绝,您必须首先检查该文件的权限是否正确,其次,该文件当前是否正在被其他应用程序使用。
- 如果异常消息声称指定文件是一个目录,那么您必须更改文件名或删除现有目录(如果该目录未被应用程序使用)。
查看下一个 link 了解更多信息 http://examples.javacodegeeks.com/java-basics/exceptions/java-io-filenotfoundexception-how-to-solve-file-not-found-exception/
我遇到了这个问题,发现让线程休眠至少一毫秒可以让文件可读。如果再次出现异常,则休眠 1000 毫秒,看看是否有效。使用
Thread.sleep(1);
并且不要忘记添加像 Eclipse 这样的 IDE 应该自动为您执行的 Throws 声明。
让我知道你得到了什么。
也许,这个问题在这里已经被问过很多次了,但是请原谅我,因为我花了几个小时也无法解决它。我正在尝试从我的目录加载一个文件并将其显示在 TextArea 上。但是,我一直得到java.io.FileNotFoundException。一开始我以为是文件权限的问题,但是我改了权限之后,还是报同样的错误。我检查了很多次路径是否正确或拼写是否正确。我什至尝试将错误堆栈跟踪的路径粘贴到我的终端,并且该路径正在运行。这是我的代码:
private void treeFileValueChanged(javax.swing.event.TreeSelectionEvent evt) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) treeFile.getLastSelectedPathComponent();
if(selectedNode==null)
JOptionPane.showMessageDialog(this, "Error");
if(selectedNode.isLeaf()){
String path = Arrays.toString(selectedNode.getUserObjectPath());
path = path.replaceAll("[\[\]\:,]","");
String[] _path = path.split(" ");
String filePath="";
int counter=1;
for (String s : _path) { // I tried to re-construct the path of the selected node/child
if(counter==_path.length){
filePath += s;
counter = 1;
}else{
filePath += s+"/";
counter++;
}
}
try {
setTextArea(filePath); //passing the filePath in string format
} catch (FileNotFoundException ex) {
Logger.getLogger(Preprocess.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Preprocess.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void setTextArea(String _filePath) throws FileNotFoundException, IOException{
File file = new File(_filePath);
if(!file.exists())
System.out.println("File not found");
String dir = System.getProperty("user.dir");
System.out.println("Current sys dir: " + dir);
System.out.println("Current abs dir: "+file.getAbsolutePath());
BufferedReader br = new BufferedReader(new FileReader(file.getName()));
try{
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null){
sb.append(line);
sb.append(System.lineSeparator());
}
textPreprocess.setText(sb.toString());
}finally{
br.close();
}
}
下面是输出:
Current sys dir: /Users/adibangun/Downloads/ThematicAnalysis
Current abs dir: /Users/adibangun/Downloads/ThematicAnalysis/Output/Sentiment/Sentiment20150629.txt
Jun 29, 2015 11:32:55 PM thematicanalysis.GUI.Preprocess treeFileValueChanged
SEVERE: null
java.io.FileNotFoundException: Sentiment20150629.txt (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
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 thematicanalysis.GUI.Preprocess.setTextArea(Preprocess.java:197)
at thematicanalysis.GUI.Preprocess.treeFileValueChanged(Preprocess.java:163)
at thematicanalysis.GUI.Preprocess.access[=11=]0(Preprocess.java:33)
at thematicanalysis.GUI.Preprocess.valueChanged(Preprocess.java:75)
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:3324)
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:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:731)
at java.awt.EventQueue.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
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)
编辑:
密码文件:/Users/adibangun/Downloads/ThematicAnalysis/Output/Sentiment
ls-ltr Sentiment20150629.txt : -rw-r--r-- 1 adibangun 员工 408555 6 月 29 日 23:13 Sentiment20150629.txt
有人知道这个问题吗?任何评论和帮助将不胜感激。非常感谢
将BufferedReader br = new BufferedReader(new FileReader(file.getName()));
替换为BufferedReader br = new BufferedReader(new FileReader(file));
在以下情况下抛出 FileNotFoundException:
- 如果异常消息提示没有这样的文件或目录,那么您必须验证指定的是否正确并且实际指向您系统中存在的文件或目录。
- 如果异常消息提示权限被拒绝,您必须首先检查该文件的权限是否正确,其次,该文件当前是否正在被其他应用程序使用。
- 如果异常消息声称指定文件是一个目录,那么您必须更改文件名或删除现有目录(如果该目录未被应用程序使用)。
查看下一个 link 了解更多信息 http://examples.javacodegeeks.com/java-basics/exceptions/java-io-filenotfoundexception-how-to-solve-file-not-found-exception/
我遇到了这个问题,发现让线程休眠至少一毫秒可以让文件可读。如果再次出现异常,则休眠 1000 毫秒,看看是否有效。使用
Thread.sleep(1);
并且不要忘记添加像 Eclipse 这样的 IDE 应该自动为您执行的 Throws 声明。 让我知道你得到了什么。