0

I've been scouring the web for a solution to this seemingly simple problem, but I always run into FileNotFoundException. I am using Java 8 on Eclipse Oxygen and can't retrieve my txt file from either absolute path or relative path. As suggested in other SO answers I got the path to the current directory, which I suppose is where the txt file is loaded from:

Path path = FileSystems.getDefault().getPath("").toAbsolutePath();

This showed my directory as E:\eclipse-java-oxygen-R-win32\HashMap

However, when I added my txt file to that project directory (containing src,bin directories) it still couldn't find the file when I wrote: Scanner input = new Scanner(new File("free.txt"))

I even tried the absolute path: Scanner input = new Scanner(new File("E://eclipse-java-oxygen-R-win32//HashMap//free.txt"));

I included a screenshot of the location of my free.txt file below.

enter image description hereYour help would be much appreciated.

I Like
  • 1,711
  • 2
  • 27
  • 52
  • 1
    Your path separator might be the issue. Have you tried with / or \\. There is also a pathSeparator constant you could use or the Path API to get rid of the necessity of a path separator – eric.v Mar 10 '18 at 17:14
  • I've tried with both, but no luck – I Like Mar 10 '18 at 17:15
  • 1
    What exact message is returned by the OS? – ernest_k Mar 10 '18 at 17:16
  • `Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type FileNotFoundException` – I Like Mar 10 '18 at 17:19
  • 1
    @st4rgut You're having compile issues. Try running a clean and then build again. Seems like you have a checked exception that you aren't catching. – ernest_k Mar 10 '18 at 17:21
  • 1
    Pretty much self explanatory. Surround your call with a try catch block and catch filenotfoundexception. You have a compile time problem because java wants you to specify how you would handle the case of an absent file and maybe some more – eric.v Mar 10 '18 at 17:22
  • ah thanks, works now. i thought my file was in the wrong place – I Like Mar 10 '18 at 17:28

2 Answers2

1

Hmm, lets try to split things up so that we know what exactly is the problem. You could also try to get a more explicit error message by printing the exact error of the File Exception. Something like this:

catch (IOException e) { 
      System.out.println("IOException caught -- "); 
      System.out.println(e.getMessage());
    }

But going further. Your line: Scanner input = new Scanner(new File("free.txt"))can be split up.

Try this:

String file_name="free.txt";
File file_input = new File(file_name);
if (!file_input.exists())
    abort("FileInput: no such source file: " + file_name);
if (!_file_input.isFile())
    abort("FileInput: can't open a directory: " + file_name);
if (!file_input.canRead())
    abort("FileInput: source file is unreadable: " + file_name);

Then run

try {
    `Scanner input = new Scanner(file_input)`
catch (FileNotFoundException e) {
        e.printStackTrace();
    }

At least this should tell you more about the error and do the debugging for you.

pastaleg
  • 1,782
  • 2
  • 17
  • 23
0

I don't know where your file is but I think this is what you're looking for. i have a file named test.json

String jsonString = new String(Files.readAllBytes(Paths.get(ClassLoader.getSystemResource("test.json").toURI())));