java FileNotFoundException: 但文件确实存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31126833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FileNotFoundException: But the file does exist
提问by bohr
Maybe, this problem has been asked many times here, but forgive me since I've spent hours and I could not fix it. I am trying to load a file from my directory and show it on a TextArea. However, I keep get java.io.FileNotFoundException. Firstly, I thought the problem is the file permission, yet, after I changed the permission, I still get the same errors. I've checked many times whether the path is correct or the spelling is correct. I even tried to paste the path from the error stacktrace to my terminal and the path is working. Here is my code:
也许,这个问题在这里被问过很多次,但请原谅我,因为我花了几个小时而无法解决它。我正在尝试从我的目录中加载一个文件并将其显示在 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();
}
}
Below is the output:
下面是输出:
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.accessThread.sleep(1);
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)
EDIT:
编辑:
pwd file: /Users/adibangun/Downloads/ThematicAnalysis/Output/Sentiment
密码文件:/Users/adibangun/Downloads/ThematicAnalysis/Output/Sentiment
ls-ltr Sentiment20150629.txt : -rw-r--r-- 1 adibangun staff 408555 29 Jun 23:13 Sentiment20150629.txt
ls-ltr Sentiment20150629.txt : -rw-r--r-- 1 adibangun 员工 408555 6 月 29 日 23:13 Sentiment20150629.txt
Does anyone know the problem? Any comment and help would really be appreciated. Many thanks
有谁知道问题所在?任何评论和帮助将不胜感激。非常感谢
回答by Alejandro Agapito Bautista
Replace BufferedReader br = new BufferedReader(new FileReader(file.getName()));
by BufferedReader br = new BufferedReader(new FileReader(file));
替换BufferedReader br = new BufferedReader(new FileReader(file.getName()));
为BufferedReader br = new BufferedReader(new FileReader(file));
FileNotFoundException is thrown if:
在以下情况下抛出 FileNotFoundException:
- If the message of the exception claims that there is no such file or directory, then you must verify that the specified is correct and actually points to a file or directory that exists in your system.
- If the message of the exception claims that permission is denied then, you must first check if the permissions of the file are correct and second, if the file is currently being used by another application.
- If the message of the exception claims that the specified file is a directory, then you must either alter the name of the file or delete the existing directory (if the directory is not being used by an application).
- 如果异常消息声称不存在这样的文件或目录,那么您必须验证指定的是否正确并且实际指向系统中存在的文件或目录。
- 如果异常消息声称权限被拒绝,则您必须首先检查文件的权限是否正确,其次,如果该文件当前正被另一个应用程序使用。
- 如果异常消息声称指定的文件是一个目录,那么您必须更改文件名或删除现有目录(如果该目录未被应用程序使用)。
check the next link for more information http://examples.javacodegeeks.com/java-basics/exceptions/java-io-filenotfoundexception-how-to-solve-file-not-found-exception/
检查下一个链接以获取更多信息http://examples.javacodegeeks.com/java-basics/exceptions/java-io-filenotfoundexception-how-to-solve-file-not-found-exception/
回答by Sina R
I had this problem and found that letting the thread sleep for at least a millisecond let the file be readable. If you get the exception again, sleep for 1000 milliseconds and see if it works. Use
我遇到了这个问题,发现让线程休眠至少一毫秒可以让文件可读。如果再次出现异常,请休眠 1000 毫秒,看看它是否有效。利用
##代码##and don't forget to add the Throws declaration which an IDE like Eclipse should do automatically for you. Let me know what you get.
并且不要忘记添加像 Eclipse 这样的 IDE 应该自动为你做的 Throws 声明。让我知道你得到了什么。