CrimsonRain
CrimsonRain

Reputation: 125

file exists returns false when file exists

I have the following code:

public static void main(String[] args){
    System.out.println(System.getProperty("user.dir"));
    File file = new File("/stuff.txt");
    System.out.println(file.exists());
}

When I run it however, the file.exists() returns as false despite that the file exists. I checked that System.getProperty("user.dir") looks at the correct folder. I think I put the files in the right place: the structure is as below:

-- filetest
    |-- FileTest.class
    |-- FileTest.java
    `-- stuff.txt

Upvotes: 0

Views: 134

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201447

You are ignoring the current user.dir and using the root folder /. To fix, remove the /. Like,

File file = new File("stuff.txt"); // <-- look for "stuff.txt" in the current folder

Upvotes: 4

Related Questions