. io .FileNotFoundException,文件未被找到。

时间:2022-06-11 08:36:16

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.txt”。这是之前的一次执行,我没有扩展。所以,我把它编辑成连贯的。它仍然不工作。

4 个解决方案

#1


9  

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”。我看不见,因为扩展被隐藏了。

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.

也要指出,任何一个/和\\工作都很好。

#2


0  

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

    正如JB Nizet在注释中指出的,错误消息提示该程序试图打开一个“图形”文件(不是路径和扩展名),这与您向我们展示的代码不兼容。您确定错误消息来自运行该代码吗?你试着去调试它(一步一步)吗?

  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.

    Windows 7 ?也许您更喜欢在一些“nice”目录中设置一个工作目录,比如C:\wk\之类的,这样您就可以排除权限问题,并且拥有更短的路径。

  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.

    关于回弹的一些答案的建议是不相关的。在Windows中,前斜杠在Java中很好用。没必要担心这个。

#3


0  

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

    }
}

#4


0  

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.

我有一个类似的问题,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.

为了找到正确的路径,我从导入项目的过程中复制了正确的路径。这解决了我的问题。

#1


9  

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”。我看不见,因为扩展被隐藏了。

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.

也要指出,任何一个/和\\工作都很好。

#2


0  

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

    正如JB Nizet在注释中指出的,错误消息提示该程序试图打开一个“图形”文件(不是路径和扩展名),这与您向我们展示的代码不兼容。您确定错误消息来自运行该代码吗?你试着去调试它(一步一步)吗?

  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.

    Windows 7 ?也许您更喜欢在一些“nice”目录中设置一个工作目录,比如C:\wk\之类的,这样您就可以排除权限问题,并且拥有更短的路径。

  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.

    关于回弹的一些答案的建议是不相关的。在Windows中,前斜杠在Java中很好用。没必要担心这个。

#3


0  

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

    }
}

#4


0  

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.

我有一个类似的问题,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.

为了找到正确的路径,我从导入项目的过程中复制了正确的路径。这解决了我的问题。