-1

I've been trying to set up a Scanner to use a File as an input, but it doesn't seem to recognize the filepath. The file exists in the same folder as my .java files.

File errorList = new File("Errors.txt");
Scanner errorIn = new Scanner(errorList);

This results in a FileNotFoundException. What am I doing wrong, and how can I fix this?

  • 1
    Screenshot your file structure please. Because i think it might need to be in one folder up or in a resources folder. – Mohammad C Oct 13 '18 at 23:23
  • Also make sure that System.out.println(errorList.getCanonicalPath()) returns what you expect. – tanderson Oct 13 '18 at 23:26
  • See [this question](https://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder). It's not an exact duplicate, since that question talks about a "resources" folder that you didn't mention -- but you should put your file into a resources dir (I'm not exactly sure how to do that in Eclipse, sorry), in which case the answer is the same. That's really the right way to do this. – yshavit Oct 13 '18 at 23:26

1 Answers1

1

One other approach you could try is, execute the below code in your eclipse (from any of your class), and see where the hello.txt is created, so you get an idea of where Java is looking for the file.

new File("hello.txt").createNewFile();

Then you could either put your Errors.txt in that location or provide the corresponding relative location.

Gopu
  • 21
  • 3