我已閱讀其他有關此問題的問題的答案,並嘗試了他們的建議,但沒有成功。在我的計劃,我有這五條線都在陸續:FileNotFoundException即使文件存在
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
的文件,據我所知,位於多個地點:
/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)
你想對文件做什麼? – user432 2014-12-03 15:29:20
Data.txt真的有一個captital D嗎?另外,當您只是創建一個新的文件時,會發生什麼情況,而不是先將它送入掃描儀?你能得到一些文件信息,還是已經失敗了?如果你能顯示你所得到的例外情況,這也會很好。 – SirRichie 2014-12-03 15:32:06
它確實有一個資本D.我修改了我的問題以包括這個觀點。你是什麼意思,只是創建一個新的文件?我是Java的新手,對它的file-io非常不熟悉。 – Bobazonski 2014-12-03 15:38:54