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

前端 未结 4 1234
旧巷少年郎
旧巷少年郎 2020-11-30 12:30

I am using Eclipse to compile and run my java codes.

Here is Error I am getting.

Exception in thread \"main\" java.io.FileNotFoundEx         


        
相关标签:
4条回答
  • 2020-11-30 13:08

    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);
                }
    
    0 讨论(0)
  • 2020-11-30 13:20

    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")
    
    0 讨论(0)
  • 2020-11-30 13:29

    In Windows try giving real path like this

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

    It worked for me.

    0 讨论(0)
  • 2020-11-30 13:33

    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");
    
    0 讨论(0)
提交回复
热议问题