Java says FileNotFoundException but file exists

前端 未结 10 1575
遇见更好的自我
遇见更好的自我 2020-11-22 10:19

I have an assignment for my CS class where it says to read a file with several test scores and asks me to sum and average them. While summing and averaging is easy, I am hav

10条回答
  •  梦谈多话
    2020-11-22 10:47

    I had this same error and solved it simply by adding the src directory that is found in Java project structure.

    String path = System.getProperty("user.dir") + "\\src\\package_name\\file_name";
    File file = new File(path);
    Scanner scanner = new Scanner(file);
    

    Notice that System.getProperty("user.dir") and new File(".").getAbsolutePath() return your project root directory path, so you have to add the path to your subdirectories and packages

提交回复
热议问题