tags:

views:

62

answers:

1

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?

+4  A: 

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

Chris Jester-Young
but why ... i'm not asking how to get the desired result, but whether there's logic to the behavior.
pstanton
Well, because "" is not a valid file name, whereas "." is (and refers to the current directory). So, the fact that "" even partially works is, in my view, purely accidental.
Chris Jester-Young
fair enough! i'd expect an exception rather than a broken object though.
pstanton