如何创建java.nio.Path?

以下代码段显示了如何创建Path。中的Path(java.nio.Path)interface表示文件系统中的某个位置,例如C:/Windows/System32或/usr/bin。

要创建一个Path我们可以使用的java.nio.Paths.get(String first, String... more)方法。在下面,您可以看到如何Path仅通过传递first字符串以及通过传递first字符串和一些varargs字符串来创建a 。

package org.nhooo.example.io;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class PathCreate {
    public static void main(String[] args) {
        // 创建一个表示Windows安装位置的路径。
        Path windows = Paths.get("C:/Windows");

        // 检查路径是否代表目录。
        if (Files.isDirectory(windows)) {
            // 做点什么
        }

        // 创建一个表示Windows程序安装位置的路径。
        Path programFiles = Paths.get("C:/Program Files");
        Files.isDirectory(programFiles);

        // 创建一个表示notepad.exe程序的路径
        Path notepad = Paths.get("C:/Windows", "System32", "notepad.exe");

        // 检查路径是否代表可执行文件。
        if (Files.isExecutable(notepad)) {
            // 做点什么
        }
    }
}