antonpp
antonpp

Reputation: 2373

Java nio. Empty path

Can anyone tell me where Paths.get("") points to?

Here is the code and the output.

public static void main(String[] args) {
    Path path = Paths.get("");
    System.out.printf("`%s`%n", path);
    System.out.printf("`%s`%n", path.normalize());
    System.out.println(Files.exists(path));
    System.out.println(Files.isExecutable(path));
}

``
``
true
true

Upvotes: 6

Views: 8629

Answers (3)

catch32
catch32

Reputation: 18582

As documentation says it has link to existing File System:

using it will imply an assumed reference to the default FileSystem and limit the utility of the calling code

You can check with small update:

public static void main(String[] args) {
   Path path = Paths.get("");

   System.out.printf("`%s`%n", path);
   System.out.printf("`%s`%n", path.normalize());
   System.out.println(Files.exists(path));
   System.out.println(Files.isExecutable(path));
        
   System.out.println(path.toFile().getAbsolutePath());
}

Last output is something like:

C:\Users\Nazar\Projects\IdeaProjects\test-project

Upvotes: 0

Benedikt Bünz
Benedikt Bünz

Reputation: 646

It maps to the directory from which you run your program. Convert it to an absolute path to test yourself. System.out.println(Paths.get("").toAbsolutePath());

Upvotes: 3

Andrew Rueckert
Andrew Rueckert

Reputation: 5215

System.out.println(Paths.get("").toAbsolutePath());

/Users/andrew/workspace/scratch

Looks like it's the current working directory. On my machine, Java is reporting that it's executable because the 'x' flag on the directory is true for the current user.

From the javadocs:

This method checks that a file exists and that this Java virtual machine has appropriate privileges to execute the file. The semantics may differ when checking access to a directory. For example, on UNIX systems, checking for execute access checks that the Java virtual machine has permission to search the directory in order to access file or subdirectories.

Upvotes: 9

Related Questions