gpt4 book ai didi

java - FileNotFoundException:但文件确实存在

转载 作者:行者123 更新时间:2023-12-01 11:21:54 24 4
gpt4 key购买 nike

也许这个问题已经被问过很多次了,但是请原谅我,因为我花了几个小时但无法解决它。我正在尝试从我的目录加载文件并将其显示在文本区域上。但是,我不断收到 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)

编辑:

密码文件:/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:

  1. 如果异常消息声称不存在此类文件或目录,则您必须验证指定的内容是否正确并且确实指向系统中存在的文件或目录。
  2. 如果异常消息声称权限被拒绝,则您必须首先检查文件的权限是否正确,然后检查该文件当前是否正在被其他应用程序使用。
  3. 如果异常消息声明指定的文件是目录,则您必须更改文件名或删除现有目录(如果应用程序未使用该目录)。

查看下一个链接以获取更多信息http://examples.javacodegeeks.com/java-basics/exceptions/java-io-filenotfoundexception-how-to-solve-file-not-found-exception/

关于java - FileNotFoundException:但文件确实存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31126833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com