pstanton
pstanton

Reputation: 36640

Java file construction - why am I getting different results?

I've just stumbled on a weird scenario and am wondering if anyone can explain this behaviour.

Case 1:

File base = new File("");
System.out.println(base.getAbsolutePath());
System.out.println(base.isDirectory());
System.out.println(base.canRead());

Result:

C:\workspace-sss\Commons
false
false

Case 2:

File base = new File("C:/workspace-sss/Commons");
System.out.println(base.getAbsolutePath());
System.out.println(base.isDirectory());
System.out.println(base.canRead());

Result:

C:\workspace-sss\Commons
true
true

If the absolute path of the two File objects are equal, why are they treated differently?

Upvotes: 2

Views: 104

Answers (1)

C. K. Young
C. K. Young

Reputation: 223013

If you used new File("."), you should get the correct results for the current directory.

Upvotes: 5

Related Questions