Exception in thread “main” java.io.FileNotFoundException: Error

佐手、 提交于 2019-11-27 05:27:23

Your file should directly be under the project folder, and not inside any other sub-folder.

So, if your project folder is MyProject, it's folder structure(not complete though) should be like: -

MyProject +- src +
          |      |
          |      +-- Your source file
          +- file.txt

It should not be under src folder.


Or, you can give the following path relative to the project folder to search for file in the src folder: -

new File("src/file.txt");

Try passing the complete path to the file, say:

new File("/usr/home/mogli/file.txt")

Or if you're in windows:

new File("C:/Users/mogli/docs/file.txt")

Either follow @rohit Jains approach or give the absolute path for your file like:

 Scanner KB = new Scanner(new File("C:/JsfProjects/Project/file1.txt"));
            while (KB.hasNext()) {
                String line = KB.nextLine();
                System.out.println(line);
            }

In Windows try giving real path like this

"C:\\Users\\mogli\\docs\\file.txt"

It worked for me.

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