java.io.FileNotFoundException,找不到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6548593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:40:25  来源:igfitidea点击:

java.io.FileNotFoundException, file not being found

javafile-iofilenotfoundexception

提问by Rauter

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.

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

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.

正如评论中所说,它正在搜索“Graph”而不是“Graph.txt”。这是我之前在没有扩展名的情况下尝试的执行。所以,我编辑它是连贯的。它仍然不起作用。

采纳答案by Rauter

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

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

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

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

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

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

回答by leonbloy

  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.

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

  2. Windows 7的?也许您更喜欢在某个“不错”的目录中设置一个工作目录,例如C:\wk\或类似的目录,以便您可以排除权限问题并拥有更短的路径。

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

回答by Danyun Liu

You need to add the try catch block.

您需要添加 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){

    }
}

回答by Thomas Esch

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, NOTmy workspace which caused the FileNotFoundException.

我对 java.io.FileNotFoundException 有类似的问题。我从电子邮件下载了一个项目,解压缩并存储在我的桌面上,而不是导致 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.

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