简体   繁体   English

Java nio。 空路径

[英]Java nio. Empty path

Can anyone tell me where Paths.get("") points to? 谁能告诉我Paths.get("")指向哪里?

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

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());

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. 在我的机器上,Java报告它是可执行的,因为目录上的'x'标志对于当前用户是真的。

From the javadocs : 来自javadocs

This method checks that a file exists and that this Java virtual machine has appropriate privileges to execute the file. 此方法检查文件是否存在,以及此Java虚拟机是否具有执行该文件的适当权限。 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. 例如,在UNIX系统上,检查执行访问权限检查Java虚拟机是否有权搜索目录以访问文件或子目录。

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 使用它将暗示对默认FileSystem的假定引用并限制调用代码的实用程序

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 C:\\用户\\纳扎尔\\项目\\ IdeaProjects \\测试项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM