Reputation: 565
I'm testing if a file exists using the new classes in the Java nio package, but I'm getting a result that seems a bug:
The following code:
Path p = Paths.get("");
System.err.println("[" + p.getFileName().toString() + "]");
System.err.println("[" + p.getParent() + "]");
System.err.println(Files.exists(p));
Produces the output:
[]
[null]
true
Why does it print true? Is this a bug or is it the expected behavior?
Upvotes: 0
Views: 472
Reputation: 718758
From the javadocs for the Path
class:
"A Path is considered to be an empty path if it consists solely of one name element that is empty. Accessing a file using an empty path is equivalent to accessing the default directory of the file system."
You have created an empty path, and the exists()
call is testing if the "default directory of the file system" exists. It does.
What you are seeing is "expected behavior".
Note that the "default directory" could mean different things depending on the host platform, but on UNIX based systems and Windows, it means the "current directory" for the JVM.
Upvotes: 0
Reputation: 36113
""
is the path where you execute your app.
Try this:
System.err.println(p.toFile().getAbsolutePath());
Then you will see where you are.
Upvotes: 1