我已经回顾了有关此问题的其他问题的答案,并尝试了他们的建议,但没有成功。在我的计划中,我连续五行:
String curDir = System.getProperty("user.dir");
System.out.println(curDir);
File f = new File("/home/brian/workspace/Color Sampler/src/Data.txt");
if(f.exists() && !f.isDirectory()) { System.out.println("success"); }
Scanner input = new Scanner(new File("/home/brian/workspace/Color Sampler/src/Data.txt"));
前四行导致输出:
/home/brian/workspace/Color Sampler
success
据我所知,该文件位于MULTIPLE位置:
/home/brian/workspace/Color Sampler
/home/brian/workspace/Color Sampler/src
/home/brian/workspace/Color Sampler/bin
尽管如此,我还是得到了例外。我已经尝试了上述所有三个文件路径。该文件也无法在任何地方打开,因此它应该是可读的。此时我已经把头发撕掉,因为我花了一个多小时就一个简单的文件名规范。
这是例外:
Exception in thread "AWT-EventQueue-1" java.lang.Error: Unresolved compilation problem:
Unhandled exception type FileNotFoundException
at WindowDestroyer.windowOpened(WindowDestroyer.java:46)
at java.awt.Window.processWindowEvent(Window.java:1972)
at javax.swing.JFrame.processWindowEvent(JFrame.java:290)
at java.awt.Window.processEvent(Window.java:1933)
at java.awt.Component.dispatchEventImpl(Component.java:4649)
at java.awt.Container.dispatchEventImpl(Container.java:2103)
at java.awt.Window.dispatchEventImpl(Window.java:2588)
at java.awt.Component.dispatchEvent(Component.java:4475)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:675)
at java.awt.EventQueue.access$300(EventQueue.java:96)
at java.awt.EventQueue$2.run(EventQueue.java:634)
at java.awt.EventQueue$2.run(EventQueue.java:632)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:108)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:119)
at java.awt.EventQueue$3.run(EventQueue.java:648)
at java.awt.EventQueue$3.run(EventQueue.java:646)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:108)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:645)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
答案 0 :(得分:3)
您的问题不是,找不到文件,问题是,您没有处理可能的FileNotFoundException
。
将代码更改为:
try (Scanner input = new Scanner(new File("/home/brian/workspace/Color Sampler/src/Data.txt"))) {
// read file and all
} catch (FileNotFoundException e) {
e.printStackTrace();
}
或在您的方法中添加throws
子句:
public void method() throws FileNotFoundException {
}
答案 1 :(得分:0)
我认为问题的关键是"未解决的编译错误"。您的项目是否在Eclipse中生成?如果是这样,根据this discussion,Eclipse可以生成不应编译的代码,因此在运行时会出错。
所有开放操作都可能产生异常。查看文档并更正您的代码,并附上适当的try-catch
条款。
试试这个:
try {
Scanner input = new Scanner(new File("/home/brian/workspace/Color Sampler/src/Data.txt"));
} catch (Exception ex) {
}
如果有效,那就是方向。
附录:有关此问题的更多信息,请访问this discussion。
答案 2 :(得分:-1)
您可能没有足够的权限来访问该文件。
首先尝试在File对象上调用exists()
,它应该返回true
。