java.io.FileNotFoundException, file not being found

久未见 提交于 2019-11-27 22:58:21

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.

  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.

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

    }
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!