FileNotFoundException:但该文件确实存在

时间:2015-06-29 22:46:20

标签: java filenotfoundexception

也许,这个问题在这里被多次询问过,但请原谅我,因为我已经花了好几个小时而无法修复它。我正在尝试从我的目录加载文件并在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$000(Preprocess.java:33)
    at thematicanalysis.GUI.Preprocess$1.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.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    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:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.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)

编辑:

pwd文件:/ Users / adibangun / Downloads / ThematicAnalysis / Output / Sentiment

ls-ltr Sentiment20150629.txt:-rw-r - r-- 1 adibangun staff 408555 29 Jun 23:13 Sentiment20150629.txt

有谁知道这个问题?任何评论和帮助将非常感激。非常感谢

2 个答案:

答案 0 :(得分:1)

BufferedReader br = new BufferedReader(new FileReader(file.getName()));替换为BufferedReader br = new BufferedReader(new FileReader(file));

如果出现以下情况,则抛出FileNotFoundException:

  1. 如果异常消息声称没有此类文件或目录,则必须验证指定的是否正确并且实际指向系统中存在的文件或目录。
  2. 如果异常消息声明权限被拒绝,则必须首先检查文件的权限是否正确,其次,如果该文件当前正由另一个应用程序使用,则必须检查。
  3. 如果异常消息声明指定的文件是目录,则必须更改文件名或删除现有目录(如果应用程序未使用该目录)。
  4. 查看下一个链接以获取更多信息http://examples.javacodegeeks.com/java-basics/exceptions/java-io-filenotfoundexception-how-to-solve-file-not-found-exception/

答案 1 :(得分:0)

我遇到了这个问题,发现让线程休眠至少一毫秒让文件可读。如果再次获得异常,请休眠1000毫秒,看看是否有效。使用

Thread.sleep(1);

并且不要忘记添加像Eclipse这样的IDE应该自动执行的Throws声明。 让我知道你得到了什么。