File not found in same folder Java

java

Solution

Because the `File` isn't where you think it is. Print the path that your program is attempting to read.

File f = new File("file.txt");
try {
    System.out.println(f.getCanonicalPath());
} catch (IOException e) {
    e.printStackTrace();
}

Per the `File.getCanonicalPath()` javadoc, A canonical pathname is both absolute and unique. The precise definition of canonical form is system-dependent.

Problem

I am trying to read a file in Java. I wrote a program and saved the file in the exact same folder as my program. Yet, I keep getting a FileNotFoundException. Here is the code: ``` public static void main(String[] args) throws IOException { Hashtable<String, Integer> ht = new Hashtable<String, Integer>(); File f = new File("file.txt"); ArrayList<String> al = readFile(f, ht); } public static ArrayList<String> readFile(File f, Hashtable<String, Integer> ht) throws IOException{ ArrayList<String> al = new ArrayList<String>(); BufferedReader br = new BufferedReader(new FileReader(f)); String line = ""; int ctr = 0; } ... return al; } ``` Here is the stack trace: ``` Exception in thread "main" java.io.FileNotFoundException: file.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at java.io.FileReader.<init>(Unknown Source) at csfhomework3.ScramAssembler.readFile(ScramAssembler.java:26) at csfhomework3.ScramAssembler.main(ScramAssembler.java:17) ``` I don't understand how the file can't be found if it's in the exact same folder as the program. I'm running the program in eclipse and I checked my run configurations for any stray arguments and there are none. Does anyone see what's wrong?

Original source

Related problems