Java file construction - why am I getting different results?

I just stumbled upon a strange scenario and I am wondering if anyone can explain this behavior.

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 is equal, why are they treated differently?

+2


source to share


1 answer


If you used new File(".")

, you should get correct results for the current directory.



+5


source







All Articles