java.io.FileNotFoundException,未找到文件

发布于 2024-11-18 05:07:51 字数 933 浏览 2 评论 0原文

我只想逐行读取文件。 这本来是很简单的,但我就是无法做到正确!

String fileName = "C:/Users/Diogo/Desktop/Krs_Grafo/Graph.txt";
FileReader file = new FileReader(fileName);
BufferedReader inputStream = new BufferedReader(file);
System.out.println(inputStream.readLine());

我不断收到错误:

Exception in thread "main" java.io.FileNotFoundException: C:\Users\Diogo\Desktop\Krs_Grafo\Graph.txt (O sistema não pode encontrar o arquivo especificado)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at java.io.FileReader.<init>(FileReader.java:41)
at krs_grafo.Krs_Grafo.main(Krs_Grafo.java:51)
Java Result: 1

系统找不到该文件,但我确信它就在那里! 我在 Windows 7 上使用 Netbeans 7.0。

有什么建议吗?

正如评论中所述,它正在搜索“Graph”而不是“Graph.txt”。这是来自之前的执行,我在没有扩展名的情况下尝试过。因此,我对其进行了编辑以使其连贯。它仍然不起作用。

I just wanted to read a file line by line.
This was meant to be simple, but i just can't get it right!

String fileName = "C:/Users/Diogo/Desktop/Krs_Grafo/Graph.txt";
FileReader file = new FileReader(fileName);
BufferedReader inputStream = new BufferedReader(file);
System.out.println(inputStream.readLine());

i keep getting the error:

Exception in thread "main" java.io.FileNotFoundException: C:\Users\Diogo\Desktop\Krs_Grafo\Graph.txt (O sistema não pode encontrar o arquivo especificado)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at java.io.FileReader.<init>(FileReader.java:41)
at krs_grafo.Krs_Grafo.main(Krs_Grafo.java:51)
Java Result: 1

The system cant find the file, but i'm sure as hell it is there!
I'm using Netbeans 7.0 on a Windows 7.

Any suggestions?

AS SAID IN THE COMMENTS, it was searching for "Graph" and not "Graph.txt". This was from a previous execution where I tried without the extension. So, I edited it to be coherent. It still doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ペ泪落弦音 2024-11-25 05:07:51

这里的问题是文件名实际上是“Graph.txt.txt”,我看不到,因为扩展名被隐藏了

感谢用户“Michael Brewer-Davis”在评论中询问“给定目录中的 cd 和 dir 的输出”。

还要指出 / 和 \\ 都可以正常工作。

The problem here is that the file name was actually "Graph.txt.txt" wich I couldn't see because the extensions were hidden.

Thanks to user "Michael Brewer-Davis" who asked in the comments for "output of cd and dir in the given directory".

Also point out that either / and \\ work just fine.

盛夏尉蓝 2024-11-25 05:07:51
  1. 正如 JB Nizet 在评论中指出的那样,错误消息暗示该程序尝试打开一个“Graph”文件(不是路径,也没有扩展名),该文件与您向我们展示的代码不兼容。您确定该错误消息来自运行该代码吗? Didi 你尝试调试它(一步一步)吗?

  2. Windows 7?也许您更愿意在一些“好”目录中设置工作目录,例如 C:\wk\ 或类似的目录,这样您就可以排除权限问题并拥有更短的路径.

  3. 一些关于反冲的答案的建议是不相关的。正斜杠在 Windows 中的 Java 中工作得很好。无需担心这一点。

  1. As JB Nizet points in a comment, the error message hints that the program tried to open a "Graph" file (not path and no extension), which is not compatible with the code you are showing us. Are you sure that that error message comes from running that code? Didi you try to debug it (step by step) ?

  2. Windows 7? Perhaps you'd prefer to set up a working directory in some "nice" directory, like C:\wk\ or something like that, so that you can rule out permission problems and have nicer-shorter paths.

  3. The suggestion of some answers about backlasshes is not relevant. Forward slashes work nice in Java in Windows. No need to worry about that.

难理解 2024-11-25 05:07:51

您需要添加 try catch 块。

public static void main(String...args){
     String fileName = "C:/Users/DY.Liu/Desktop/Krs_Grafo/Graph.txt";
    try{
        FileReader file = new FileReader(fileName);
        BufferedReader inputStream = new BufferedReader(file);
        System.out.println(inputStream.readLine());
    } catch (FileNotFoundException e){
        e.printStackTrace();

    } catch (IOException e){

    }
}

You need to add the try catch block.

public static void main(String...args){
     String fileName = "C:/Users/DY.Liu/Desktop/Krs_Grafo/Graph.txt";
    try{
        FileReader file = new FileReader(fileName);
        BufferedReader inputStream = new BufferedReader(file);
        System.out.println(inputStream.readLine());
    } catch (FileNotFoundException e){
        e.printStackTrace();

    } catch (IOException e){

    }
}
大海や 2024-11-25 05:07:51

我对 java.io.FileNotFoundException 也有类似的问题。我从电子邮件下载了一个项目,解压缩并存储在我的桌面上,而不是我的工作区,这导致了 FileNotFoundException。

为了获得正确的路径,我复制了导入项目时显示的确切路径。这解决了我的问题。

I had a similar problem with a java.io.FileNotFoundException. I had downloaded a project from an e-mail, unzipped and stored on my Desktop, NOT my workspace which caused the FileNotFoundException.

To get the right path, I copied down the exact path from what was shown when I imported the project. and this fixed the problem for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文