gpt4 book ai didi

java - 即使文件存在 FileNotFoundException

转载 作者:行者123 更新时间:2023-12-02 05:11:47 26 4
gpt4 key购买 nike

我已经查看了有关此问题的其他答案,并尝试了他们的建议,但没有成功。在我的程序中,我连续有这五行:

    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)情况如下:

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)

最佳答案

您的问题不是找不到文件,而是您没有处理可能的 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 {

}

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

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