也许,这个问题在这里被多次询问过,但请原谅我,因为我已经花了好几个小时而无法修复它。我正在尝试从我的目录加载文件并在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
有谁知道这个问题?任何评论和帮助将非常感激。非常感谢
答案 0 :(得分:1)
将BufferedReader br = new BufferedReader(new FileReader(file.getName()));
替换为BufferedReader br = new BufferedReader(new FileReader(file));
如果出现以下情况,则抛出FileNotFoundException:
查看下一个链接以获取更多信息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声明。 让我知道你得到了什么。