AJW AJW - 1 year ago 95
Java Question

Java file path in Linux

I have a rather silly question, but I haven't been able to find a solution for this:

When I try and read a file I get a "file not found error" is runtime. It compiled the file though.

I am on Linux, so I use the statement something like:

Scanner s = new Scanner(new File("home/me/java/ex.txt"));

and it gives me a runtime rror:

/home/me/javaException in thread "main" java.io.FileNotFoundException: home/me/java/ex.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:137)
at java.util.Scanner.<init>(Scanner.java:653)
at test.main(test.java:14)

I tried changing every possible thing along the lines of filenames, but nothing seems to work.

Any clues as to why this is happening? where does java look for files by default?

Answer Source

Looks like you are missing a leading slash. Perhaps try:

Scanner s = new Scanner(new File("/home/me/java/ex.txt"));

(as to where it looks for files by default, it is where the JVM is run from for relative paths like the one you have in your question)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download