简体   繁体   English

java.io.FileNotFoundException,找不到文件

[英]java.io.FileNotFoundException, file not being found

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".正如评论中所说,它正在搜索“Graph”而不是“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.它仍然不起作用。

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.还要指出 / 和 \\ 都可以正常工作。

I had a similar problem with a java.io.FileNotFoundException.我在 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.我从电子邮件中下载了一个项目,解压缩并存储在我的桌面上,而不是导致 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.这解决了我的问题。

  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.正如 JB Nizet 在评论中指出的那样,错误消息暗示程序试图打开一个“图形”文件(不是路径也没有扩展名),该文件与您向我们展示的代码不兼容。 Are you sure that that error message comes from running that code?您确定该错误消息来自运行该代码吗? Didi you try to debug it (step by step)? Didi您尝试调试它(一步一步)?

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

  3. The suggestion of some answers about backlasshes is not relevant.关于反弹的一些答案的建议是不相关的。 Forward slashes work nice in Java in Windows.正斜杠在 Windows 中的 Java 中工作得很好。 No need to worry about that.无需担心。

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){

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM