Reputation: 29
I'm writing a java program for class that reads in from a txt file. I placed the text file in the package called Assignment3pckg and use a scanner like so:
Scanner s = new Scanner( new File("./src/Assignment3pckg/studentdata.txt") );
But it just keeps giving me
java.io.FileNotFoundException: .\src\Assignment3pckg\studentdata.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Assignment3pckg.TestHW3.readStudentDataFromFile(TestHW3.java:25)
at Assignment3pckg.TestHW3.main(TestHW3.java:14)
Any insight would be greatly appreciated!
Upvotes: 0
Views: 91
Reputation: 31299
You are using a relative path (as opposed to an absolute path) for your file. Relative paths are resolved relative to the current working directory of the Java process when you run it.
Where this directory is depends on how you run Java. You can find out by printing it:
System.out.println(new File(".").getAbsolutePath());
You can resolve it by:
C:\
or another drive letter)Note though that in Java, you have to escape backslashes in a String. So C:\myproject\src\Assignment3pckg\studentdata.txt
becomes "C:\\myproject\\src\\Assignment3pckg\\studentdata.txt"
as a Java String. Or "C:/myproject/src/Assignment3pckg/studentdata.txt"
as Windows doesn't mind forward slashes instead of backslashes either.
Upvotes: 2