Java : Path 示例

Path (Java SE 22 & JDK 22) 示例。
您将在大多数 Path 方法中找到代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

可用于在文件系统中定位文件的对象。它通常表示与系统相关的文件路径。 (机器翻译)

Class diagram

final var path = Path.of("D:", "java-work", "sample.txt");
System.out.println(path); // "D:\java-work\sample.txt"

System.out.println(Files.exists(path)); // false

Files.writeString(path, """
        aaa
        bbb
        ccc
        """);

System.out.println(Files.exists(path)); // true

final var str = Files.readString(path);
System.out.println(str);

// Result
// ↓
//aaa
//bbb
//ccc

Methods

int compareTo (Path other)

按字典顺序比较两个抽象路径。 (机器翻译)

System.out.println(System.getProperty("os.name")); // Windows 10

// Filename comparisons are case-insensitive on Windows.
final var path1 = Path.of("aaa.txt");
final var path2 = Path.of("AAA.txt");

System.out.println(path1); // "aaa.txt"
System.out.println(path2); // "AAA.txt"
System.out.println(path1.compareTo(path2)); // 0
final var path1 = Path.of("aaa.txt");
final var path2 = Path.of("bbb.txt");

System.out.println(path1); // "aaa.txt"
System.out.println(path2); // "bbb.txt"
System.out.println(path1.compareTo(path2)); // -1
System.out.println(path2.compareTo(path1)); // 1
final var path1 = Path.of("12.txt");
final var path2 = Path.of("123.txt");

System.out.println(path1); // "12.txt"
System.out.println(path2); // "123.txt"
System.out.println(path1.compareTo(path2)); // -5
System.out.println(path2.compareTo(path1)); // 5
final var path1 = Path.of("99.jpg");
final var path2 = Path.of("123.jpg");

System.out.println(path1); // "99.jpg"
System.out.println(path2); // "123.jpg"
System.out.println(path1.compareTo(path2)); // 8
System.out.println(path2.compareTo(path1)); // -8
final var path1 = Path.of("dir1", "a.txt");
final var path2 = Path.of("dir1", "b.txt");
final var path3 = Path.of("dir2", "a.txt");
final var path4 = Path.of("dir2", "b.txt");

System.out.println(path1); // "dir1\a.txt"
System.out.println(path2); // "dir1\b.txt"
System.out.println(path3); // "dir2\a.txt"
System.out.println(path4); // "dir2\b.txt"

System.out.println(path1.compareTo(path2)); // -1
System.out.println(path1.compareTo(path3)); // -1
System.out.println(path1.compareTo(path4)); // -1

System.out.println(path2.compareTo(path1)); // 1
System.out.println(path2.compareTo(path3)); // -1
System.out.println(path2.compareTo(path4)); // -1

System.out.println(path3.compareTo(path1)); // 1
System.out.println(path3.compareTo(path2)); // 1
System.out.println(path3.compareTo(path4)); // -1

System.out.println(path4.compareTo(path1)); // 1
System.out.println(path4.compareTo(path2)); // 1
System.out.println(path4.compareTo(path3)); // 1
final var path1 = Path.of("dir");
final var path2 = Path.of("dir", "a.txt");

System.out.println(path1); // "dir"
System.out.println(path2); // "dir\a.txt"
System.out.println(path1.compareTo(path2)); // -6
System.out.println(path2.compareTo(path1)); // 6
final var path1 = Path.of("dir", "a.txt");
final var path2 = Path.of("dir", "dir2", "..", "a.txt");

System.out.println(path1); // "dir\a.txt"
System.out.println(path2); // "dir\dir2\..\a.txt"
System.out.println(path1.compareTo(path2)); // -3
System.out.println(path2.compareTo(path1)); // 3

final var path3 = path2.normalize();
System.out.println(path3); // "dir\a.txt"
System.out.println(path1.compareTo(path3)); // 0

default boolean endsWith (String other)

测试此路径是否以 Path 结尾,该 Path 通过转换给定的路径字符串构造,方式与 endsWith(Path) 方法指定的方式完全相同。 (机器翻译)

final var path = Path.of("foo", "bar");
System.out.println(path); // "foo\bar"

System.out.println(path.endsWith("r")); // false
System.out.println(path.endsWith("ar")); // false
System.out.println(path.endsWith("bar")); // true
System.out.println(path.endsWith("\\bar")); // false
System.out.println(path.endsWith("o\\bar")); // false
System.out.println(path.endsWith("oo\\bar")); // false
System.out.println(path.endsWith("foo\\bar")); // true
System.out.println(path.endsWith("Zfoo\\bar")); // false

boolean endsWith (Path other)

测试此路径是否以给定路径结束。 (机器翻译)

final var path = Path.of("foo", "bar");
System.out.println(path); // "foo\bar"

System.out.println(path.endsWith(Path.of("r"))); // false
System.out.println(path.endsWith(Path.of("ar"))); // false
System.out.println(path.endsWith(Path.of("bar"))); // true
System.out.println(path.endsWith(Path.of("o", "bar"))); // false
System.out.println(path.endsWith(Path.of("oo", "bar"))); // false
System.out.println(path.endsWith(Path.of("foo", "bar"))); // true
System.out.println(path.endsWith(Path.of("Z", "foo", "bar"))); // false

boolean equals (Object other)

测试此路径是否与给定的对象相等。 (机器翻译)

System.out.println(System.getProperty("os.name")); // Windows 10

final var path = Path.of("aaa.txt");
System.out.println(path); // "aaa.txt"

// Filename comparisons are case-insensitive on Windows.
System.out.println(path.equals(Path.of("aaa.txt"))); // true
System.out.println(path.equals(Path.of("AAA.TXT"))); // true
System.out.println(path.equals(Path.of("bbb.txt"))); // false
final var path = Path.of("dir", "aaa.txt");
System.out.println(path); // "dir\aaa.txt"

System.out.println(path.equals(Path.of("dir", "aaa.txt"))); // true
System.out.println(path.equals(Path.of("DIR", "AAA.TXT"))); // true
System.out.println(path.equals(Path.of("aaa.txt"))); // false
System.out.println(path.equals(Path.of("dir"))); // false
System.out.println(path.equals(Path.of("dir", "bbb.txt"))); // false
System.out.println(path.equals(Path.of("xxx", "aaa.txt"))); // false

Path getFileName ()

以 Path 对象的形式返回此路径表示的文件或目录的名称。 (机器翻译)

final var path1 = Path.of("dir");
System.out.println(path1); // "dir"
System.out.println(path1.getFileName()); // "dir"

final var path2 = Path.of("dir", "aaa.txt");
System.out.println(path2); // "dir\aaa.txt"
System.out.println(path2.getFileName()); // "aaa.txt"

final var path3 = Path.of("dir", "dir2", "aaa.txt");
System.out.println(path3); // "dir\dir2\aaa.txt"
System.out.println(path3.getFileName()); // "aaa.txt"

final var path4 = Path.of("D:");
System.out.println(path4); // "D:"
System.out.println(path4.getFileName()); // null

final var path5 = Path.of("D:", "java-work", "aaa.txt");
System.out.println(path5); // "D:\java-work\aaa.txt"
System.out.println(path5.getFileName()); // "aaa.txt"

final var path6 = Path.of("..");
System.out.println(path6); // ".."
System.out.println(path6.getFileName()); // ".."

FileSystem getFileSystem ()

返回创建此对象的文件系统。 (机器翻译)

Please see also : FileSystem (Java SE 22 & JDK 22)

System.out.println(System.getProperty("os.name")); // Windows 10

final var path = Path.of("aaa.txt");
System.out.println(path); // "aaa.txt"

final var fileSystem = path.getFileSystem();
System.out.println(fileSystem); // sun.nio.fs.WindowsFileSystem@2657d4dd

Path getName (int index)

将此路径的名称元素作为 Path 对象返回。 (机器翻译)

final var path1 = Path.of("dir1", "dir2", "aaa.txt");
System.out.println(path1); // "dir1\dir2\aaa.txt"

//path1.getName(-1); // IllegalArgumentException
System.out.println(path1.getName(0)); // "dir1"
System.out.println(path1.getName(1)); // "dir2"
System.out.println(path1.getName(2)); // "aaa.txt"
//path1.getName(3); // IllegalArgumentException

final var path2 = Path.of("D:", "java-work", "aaa.txt");
System.out.println(path2); // "D:\java-work\aaa.txt"
System.out.println(path2.getName(0)); // "java-work"
System.out.println(path2.getName(1)); // "aaa.txt"
//path2.getName(2); // IllegalArgumentException

final var path3 = Path.of("aaa.txt");
System.out.println(path3); // "aaa.txt"
System.out.println(path3.getName(0)); // "aaa.txt"

final var path4 = Path.of("dir", "..", ".", "aaa.txt");
System.out.println(path4); // "dir\..\.\aaa.txt"
System.out.println(path4.getName(0)); // "dir"
System.out.println(path4.getName(1)); // ".."
System.out.println(path4.getName(2)); // "."
System.out.println(path4.getName(3)); // "aaa.txt"

int getNameCount ()

返回路径中名称元素的数量。 (机器翻译)

final var path1 = Path.of("dir1", "dir2", "aaa.txt");
System.out.println(path1); // "dir1\dir2\aaa.txt"
System.out.println(path1.getNameCount()); // 3

final var path2 = Path.of("D:");
System.out.println(path2); // "D:"
System.out.println(path2.getNameCount()); // 0

final var path3 = Path.of("D:", "java-work", "aaa.txt");
System.out.println(path3); // "D:\java-work\aaa.txt"
System.out.println(path3.getNameCount()); // 2

final var path4 = Path.of("aaa.txt");
System.out.println(path4); // "aaa.txt"
System.out.println(path4.getNameCount()); // 1

final var path5 = Path.of("dir", "..", ".", "aaa.txt");
System.out.println(path5); // "dir\..\.\aaa.txt"
System.out.println(path5.getNameCount()); // 4

Path getParent ()

返回父路径,如果此路径没有父路径,则返回 null。 (机器翻译)

final var path1 = Path.of("dir1", "dir2", "aaa.txt");
System.out.println(path1); // "dir1\dir2\aaa.txt"
System.out.println(path1.getParent()); // "dir1\dir2"
System.out.println(path1.getParent().getParent()); // "dir1"
System.out.println(path1.getParent().getParent().getParent()); // null

final var path2 = Path.of("D:", "java-work", "aaa.txt");
System.out.println(path2); // "D:\java-work\aaa.txt"
System.out.println(path2.getParent()); // "D:\java-work"
System.out.println(path2.getParent().getParent()); // "D:\"
System.out.println(path2.getParent().getParent().getParent()); // null

final var path3 = Path.of("aaa.txt");
System.out.println(path3); // "aaa.txt"
System.out.println(path3.getParent()); // null

final var path4 = Path.of("dir", "..", ".", "aaa.txt");
System.out.println(path4); // "dir\..\.\aaa.txt"
System.out.println(path4.getParent()); // "dir\..\."
System.out.println(path4.getParent().getParent()); // "dir\.."
System.out.println(path4.getParent().getParent().getParent()); // "dir"
System.out.println(path4.getParent().getParent().getParent().getParent()); // null

Path getRoot ()

返回此路径的根组件作为 Path 对象,如果此路径没有根组件,则返回 null。 (机器翻译)

final var path1 = Path.of("dir1", "dir2", "aaa.txt");
System.out.println(path1); // "dir1\dir2\aaa.txt"
System.out.println(path1.getRoot()); // null

final var path2 = Path.of("D:", "java-work", "aaa.txt");
System.out.println(path2); // "D:\java-work\aaa.txt"
System.out.println(path2.getRoot()); // "D:\"

final var path3 = Path.of("aaa.txt");
System.out.println(path3); // "aaa.txt"
System.out.println(path3.getRoot()); // null

final var path4 = Path.of("dir", "..", ".", "aaa.txt");
System.out.println(path4); // "dir\..\.\aaa.txt"
System.out.println(path4.getRoot()); // null

int hashCode ()

计算此路径的哈希码。 (机器翻译)

System.out.println(System.getProperty("os.name")); // Windows 10

// Filename comparisons are case-insensitive on Windows.
System.out.println(Path.of("aaa.txt").hashCode()); // -519425277
System.out.println(Path.of("AAA.TXT").hashCode()); // -519425277
System.out.println(Path.of("dir1", "dir2", "aaa.txt").hashCode()); // 1707759364
System.out.println(Path.of("D:", "java-work", "aaa.txt").hashCode()); // 1186026781

boolean isAbsolute ()

告诉该路径是否是绝对路径。 (机器翻译)

final var path1 = Path.of("dir1", "dir2", "aaa.txt");
System.out.println(path1); // "dir1\dir2\aaa.txt"
System.out.println(path1.isAbsolute()); // false

final var path2 = Path.of("D:");
System.out.println(path2); // "D:"
System.out.println(path2.isAbsolute()); // false

final var path3 = Path.of("D:", "java-work", "aaa.txt");
System.out.println(path3); // "D:\java-work\aaa.txt"
System.out.println(path3.isAbsolute()); // true

final var path4 = Path.of("aaa.txt");
System.out.println(path4); // "aaa.txt"
System.out.println(path4.isAbsolute()); // false

final var path5 = Path.of("dir", "..", ".", "aaa.txt");
System.out.println(path5); // "dir\..\.\aaa.txt"
System.out.println(path5.isAbsolute()); // false

default Iterator<Path> iterator ()

返回此路径的名称元素的迭代器。 (机器翻译)

final var path = Path.of("dir1", "dir2", "aaa.txt");
System.out.println(path); // "dir1\dir2\aaa.txt"

final var it = path.iterator();

System.out.println("-- forEachRemaining --");
it.forEachRemaining(System.out::println);

// Result
// ↓
//-- forEachRemaining --
//dir1
//dir2
//aaa.txt
final var path = Path.of("D:");
System.out.println(path); // "D:"

final var it = path.iterator();

System.out.println("-- forEachRemaining --");
it.forEachRemaining(System.out::println);

// Result
// ↓
//-- forEachRemaining --
final var path = Path.of("D:", "java-work", "aaa.txt");
System.out.println(path); // "D:\java-work\aaa.txt"

final var it = path.iterator();

System.out.println("-- forEachRemaining --");
it.forEachRemaining(System.out::println);

// Result
// ↓
//-- forEachRemaining --
//java-work
//aaa.txt
final var path = Path.of("aaa.txt");
System.out.println(path); // "aaa.txt"

final var it = path.iterator();

System.out.println("-- forEachRemaining --");
it.forEachRemaining(System.out::println);

// Result
// ↓
//-- forEachRemaining --
//aaa.txt
final var path = Path.of("dir", "..", ".", "aaa.txt");
System.out.println(path); // "dir\..\.\aaa.txt"

final var it = path.iterator();

System.out.println("-- forEachRemaining --");
it.forEachRemaining(System.out::println);

// Result
// ↓
//-- forEachRemaining --
//dir
//..
//.
//aaa.txt

Path normalize ()

返回一条该路径,该路径是该路径中消除了冗余名称元素的路径。 (机器翻译)

final var path1 = Path.of("dir1", "dir2", "aaa.txt");
System.out.println(path1); // "dir1\dir2\aaa.txt"
System.out.println(path1.normalize()); // "dir1\dir2\aaa.txt"

final var path2 = Path.of("dir1", "dir2", "..", "aaa.txt");
System.out.println(path2); // "dir1\dir2\..\aaa.txt"
System.out.println(path2.normalize()); // "dir1\aaa.txt"

final var path3 = Path.of("dir1", "dir2", "..", "..", "aaa.txt");
System.out.println(path3); // "dir1\dir2\..\..\aaa.txt"
System.out.println(path3.normalize()); // "aaa.txt"

final var path4 = Path.of("dir1", "dir2", "..", "..", "..", "aaa.txt");
System.out.println(path4); // "dir1\dir2\..\..\..\aaa.txt"
System.out.println(path4.normalize()); // "..\aaa.txt"

final var path5 = Path.of("D:", "java-work", "aaa.txt");
System.out.println(path5); // "D:\java-work\aaa.txt"
System.out.println(path5.normalize()); // "D:\java-work\aaa.txt"

final var path6 = Path.of("D:", "java-work", "..", "aaa.txt");
System.out.println(path6); // "D:\java-work\..\aaa.txt"
System.out.println(path6.normalize()); // "D:\aaa.txt"

final var path7 = Path.of("D:", "java-work", "..", "..", "aaa.txt");
System.out.println(path7); // "D:\java-work\..\..\aaa.txt"
System.out.println(path7.normalize()); // "D:\aaa.txt"

static Path of (String first, String... more)

通过转换路径字符串或连接起来形成路径字符串的字符串序列来返回路径。 (机器翻译)

final var path1 = Path.of("dir1", "dir2", "aaa.txt");
System.out.println(path1); // "dir1\dir2\aaa.txt"

final var path2 = Path.of("D:");
System.out.println(path2); // "D:"

final var path3 = Path.of("D:", "java-work", "aaa.txt");
System.out.println(path3); // "D:\java-work\aaa.txt"

final var path4 = Path.of("aaa.txt");
System.out.println(path4); // "aaa.txt"

final var path5 = Path.of("dir", "..", ".", "aaa.txt");
System.out.println(path5); // "dir\..\.\aaa.txt"

static Path of (URI uri)

通过转换 URI 返回路径。 (机器翻译)

final var uri = URI.create("file:///D:/java-work/aaa.txt");
System.out.println(uri); // "file:///D:/java-work/aaa.txt"

final var path = Path.of(uri);
System.out.println(path); // "D:\java-work\aaa.txt"
final var uri = URI.create("http://example.com");
System.out.println(uri); // "http://example.com"

try {
    final var path = Path.of(uri);
} catch (FileSystemNotFoundException e) {
    System.out.println(e);
}

// Result
// ↓
//java.nio.file.FileSystemNotFoundException: Provider "http" not installed

default WatchKey register (WatchService watcher, WatchEvent.Kind<?>... events)

使用监视服务注册此路径下的文件。 (机器翻译)

try (final var executor = Executors.newSingleThreadScheduledExecutor()) {

    final var targetDir = Path.of("D:", "java-work");
    System.out.println(targetDir); // "D:\java-work"

    final var targetFile = targetDir.resolve("aaa.txt");
    System.out.println(targetFile); // "D:\java-work\aaa.txt"
    System.out.println(Files.exists(targetFile)); // false

    executor.schedule(() -> {
        try {
            Files.createFile(targetFile);
        } catch (IOException e) {
            System.out.println("IOException!");
        }
    }, 3, TimeUnit.SECONDS);

    try (final var watchService = FileSystems.getDefault().newWatchService()) {

        final var watchKey = targetDir.register(watchService, ENTRY_CREATE);

        // Wait until an event occurs.
        final var takeKey = watchService.take();
        System.out.println(watchKey.equals(takeKey)); // true

        final var contexts = takeKey.pollEvents().stream()
                .map(WatchEvent::context)
                .toList();
        System.out.println(contexts); // [aaa.txt]
        System.out.println(Files.exists(targetFile)); // true
    }
}

WatchKey register (WatchService watcher, WatchEvent.Kind<?>[] events, WatchEvent.Modifier... modifiers)

使用监视服务注册此路径下的文件。 (机器翻译)

Please see :

Path relativize (Path other)

构造此路径和给定路径之间的相对路径。 (机器翻译)

final var path = Path.of("D:", "java-work");
System.out.println(path); // "D:\java-work"

final var ret1 = path.relativize(Path.of("D:", "java-work", "aaa.txt"));
System.out.println(ret1); // "aaa.txt"

final var ret2 = path.relativize(Path.of("D:", "java-work", "dir", "bbb.txt"));
System.out.println(ret2); // "dir\bbb.txt"
final var path = Path.of("D:", "java-work", "aaa.txt");
System.out.println(path); // "D:\java-work\aaa.txt"

final var ret1 = path.relativize(Path.of("D:", "java-work"));
System.out.println(ret1); // ".."

final var ret2 = path.relativize(Path.of("D:", "xxx", "ccc.txt"));
System.out.println(ret2); // "..\..\xxx\ccc.txt"
final var path = Path.of("dir1", "dir2");
System.out.println(path); // "dir1\dir2"

final var ret1 = path.relativize(Path.of("dir1"));
System.out.println(ret1); // ".."

final var ret2 = path.relativize(Path.of("dir1", "dir2"));
System.out.println(ret2); // ""

final var ret3 = Path.of("dir1", "dir2", "aaa.txt");
System.out.println(path.relativize(ret3)); // "aaa.txt"

final var ret4 = path.relativize(Path.of("..", "dir1", "dir2", "aaa.txt"));
System.out.println(ret4); // "..\..\..\dir1\dir2\aaa.txt"

default Path resolve (String other)

将给定的路径字符串转换为路径,并按照 resolve 方法指定的方式根据此路径进行解析。 (机器翻译)

final var path = Path.of("dir1");
System.out.println(path); // "dir1"
System.out.println(path.resolve("dir2")); // "dir1\dir2"
System.out.println(path.resolve("dir2").resolve("aaa.txt")); // "dir1\dir2\aaa.txt"
System.out.println(path.resolve("..")); // "dir1\.."
System.out.println(path.resolve("")); // "dir1"

default Path resolve (String first, String... more)

将路径字符串转换为路径,根据此路径解析该路径,然后对任何其他路径字符串迭代执行相同的过程。 (机器翻译)

final var path = Path.of("dir1");
System.out.println(path); // "dir1"
System.out.println(path.resolve("dir2")); // "dir1\dir2"
System.out.println(path.resolve("dir2", "aaa.txt")); // "dir1\dir2\aaa.txt"

Path resolve (Path other)

根据此路径解析给定的路径。 (机器翻译)

final var path = Path.of("dir1");
System.out.println(path); // "dir1"
System.out.println(path.resolve(Path.of("aaa.txt"))); // "dir1\aaa.txt"
System.out.println(path.resolve(Path.of("dir2", "dir3"))); // "dir1\dir2\dir3"
System.out.println(path.resolve(Path.of(".."))); // "dir1\.."
System.out.println(path.resolve(Path.of(""))); // "dir1"

final var absolutePath = Path.of("D:", "java-work", "aaa.txt");
System.out.println(absolutePath); // "D:\java-work\aaa.txt"
System.out.println(path.resolve(absolutePath)); // "D:\java-work\aaa.txt"

default Path resolve (Path first, Path... more)

解析此路径下的路径,然后迭代解析任何其他路径。 (机器翻译)

final var dir1 = Path.of("dir1");
System.out.println(dir1); // "dir1"

final var dir2 = Path.of("dir2");
System.out.println(dir2); // "dir2"

final var file = Path.of("aaa.txt");
System.out.println(file); // "aaa.txt"

System.out.println(dir1.resolve(file)); // "dir1\aaa.txt"
System.out.println(dir1.resolve(dir2, file)); // "dir1\dir2\aaa.txt"

default Path resolveSibling (String other)

将给定的路径字符串转换为 Path,并按照 resolveSibling 方法指定的方式根据此路径的父路径进行解析。 (机器翻译)

final var path = Path.of("dir1", "dir2", "foo");
System.out.println(path); // "dir1\dir2\foo"
System.out.println(path.resolveSibling("bar")); // "dir1\dir2\bar"
System.out.println(path.resolveSibling("..")); // "dir1\dir2\.."
System.out.println(path.resolveSibling("")); // "dir1\dir2"

default Path resolveSibling (Path other)

根据该路径的父路径解析给定的路径。 (机器翻译)

final var path = Path.of("dir1", "dir2", "foo");
System.out.println(path); // "dir1\dir2\foo"
System.out.println(path.resolveSibling(Path.of("bar"))); // "dir1\dir2\bar"
System.out.println(path.resolveSibling(Path.of("aaa", "bbb"))); // "dir1\dir2\aaa\bbb"
System.out.println(path.resolveSibling(Path.of("..", "bbb"))); // "dir1\dir2\..\bbb"
System.out.println(path.resolveSibling(Path.of(""))); // "dir1\dir2"

final var absolutePath = Path.of("D:", "java-work", "aaa.txt");
System.out.println(absolutePath); // "D:\java-work\aaa.txt"
System.out.println(path.resolveSibling(absolutePath)); // "D:\java-work\aaa.txt"

default boolean startsWith (String other)

测试此路径是否以 Path 开头,该 Path 通过转换给定的路径字符串构造,方式与 startsWith(Path) 方法指定的方式完全相同。 (机器翻译)

final var path = Path.of("foo", "bar");
System.out.println(path); // "foo\bar"

System.out.println(path.startsWith("f")); // false
System.out.println(path.startsWith("fo")); // false
System.out.println(path.startsWith("foo")); // true
System.out.println(path.startsWith("foo\\")); // true
System.out.println(path.startsWith("foo\\b")); // false
System.out.println(path.startsWith("foo\\ba")); // false
System.out.println(path.startsWith("foo\\bar")); // true
System.out.println(path.startsWith("foo\\barZ")); // false

boolean startsWith (Path other)

测试此路径是否以给定的路径开始。 (机器翻译)

final var path = Path.of("foo", "bar");
System.out.println(path); // "foo\bar"

System.out.println(path.startsWith(Path.of("f"))); // false
System.out.println(path.startsWith(Path.of("fo"))); // false
System.out.println(path.startsWith(Path.of("foo"))); // true
System.out.println(path.startsWith(Path.of("foo", "b"))); // false
System.out.println(path.startsWith(Path.of("foo", "ba"))); // false
System.out.println(path.startsWith(Path.of("foo", "bar"))); // true
System.out.println(path.startsWith(Path.of("foo", "bar", "Z"))); // false

Path subpath (int beginIndex, int endIndex)

返回一个相对路径,它是此路径的名称元素的子序列。 (机器翻译)

final var path1 = Path.of("dir1", "dir2", "aaa.txt");
System.out.println(path1); // "dir1\dir2\aaa.txt"

//path1.subpath(0, 0); // IllegalArgumentException
System.out.println(path1.subpath(0, 1)); // "dir1"
System.out.println(path1.subpath(0, 2)); // "dir1\dir2"
System.out.println(path1.subpath(0, 3)); // "dir1\dir2\aaa.txt"
//path1.subpath(0, 4); // IllegalArgumentException
//path1.subpath(1, 1); // IllegalArgumentException
System.out.println(path1.subpath(1, 2)); // "dir2"
System.out.println(path1.subpath(1, 3)); // "dir2\aaa.txt"
System.out.println(path1.subpath(2, 3)); // "aaa.txt"

final var path2 = Path.of("D:", "java-work", "aaa.txt");
System.out.println(path2); // "D:\java-work\aaa.txt"

System.out.println(path2.subpath(0, 1)); // "java-work"
System.out.println(path2.subpath(0, 2)); // "java-work\aaa.txt"
//path2.subpath(0, 3); // IllegalArgumentException

Path toAbsolutePath ()

返回一个 Path 对象,表示此路径的绝对路径。 (机器翻译)

final var userDir = System.getProperty("user.dir");
System.out.println(userDir); // "D:\java-work"

final var path1 = Path.of("aaa.txt");
System.out.println(path1); // "aaa.txt"
System.out.println(path1.toAbsolutePath()); // "D:\java-work\aaa.txt"

final var path2 = Path.of("..", "aaa.txt");
System.out.println(path2); // "..\aaa.txt"
System.out.println(path2.toAbsolutePath()); // "D:\java-work\..\aaa.txt"

final var path3 = Path.of("D:", "xxx", "bbb.txt");
System.out.println(path3); // "D:\xxx\bbb.txt"
System.out.println(path3.toAbsolutePath()); // "D:\xxx\bbb.txt"

default File toFile ()

返回代表此路径的 File 对象。 (机器翻译)

final var path = Path.of("D:", "java-work", "aaa.txt");
System.out.println(path); // "D:\java-work\aaa.txt"

final var file = path.toFile();
System.out.println(file); // "D:\java-work\aaa.txt"

System.out.println(file.createNewFile()); // true

Path toRealPath (LinkOption... options)

返回现有文件的真实路径。 (机器翻译)

final var userDir = System.getProperty("user.dir");
System.out.println(userDir); // "D:\java-work"

final var path = Path.of("aaa.txt");
System.out.println(path); // "aaa.txt"
System.out.println(Files.exists(path)); // false

try {
    path.toRealPath();
} catch (NoSuchFileException e) {
    System.out.println("NoSuchFileException!");
}

// Result
// ↓
//NoSuchFileException!

Files.createFile(path);
System.out.println(Files.exists(path)); // true

final var ret = path.toRealPath();
System.out.println(ret); // "D:\java-work\aaa.txt"
final var path = Path.of("D:", "java-work", "aaa.txt");
Files.createFile(path);

final var link = Path.of("D:", "java-work", "link.txt");
Files.createSymbolicLink(link, path);

// --- PowerShell ---
//PS D:\java-work> ls
// ...
//Mode                 LastWriteTime         Length Name
//----                 -------------         ------ ----
//-a---          xxxx/xx/xx    xx:xx              0 aaa.txt
//la---          xxxx/xx/xx    xx:xx              0 link.txt -> D:\java-work\aaa.txt

System.out.println(path); // "D:\java-work\aaa.txt"
System.out.println(path.toRealPath()); // "D:\java-work\aaa.txt"
System.out.println(path.toRealPath(LinkOption.NOFOLLOW_LINKS)); // "D:\java-work\aaa.txt"

System.out.println(link); // "D:\java-work\link.txt"
System.out.println(link.toRealPath()); // "D:\java-work\aaa.txt"
System.out.println(link.toRealPath(LinkOption.NOFOLLOW_LINKS)); // "D:\java-work\link.txt"
// Filename comparisons are case-insensitive on Windows.
final var path = Path.of("D:", "java-work", "aaa.txt");
Files.createFile(path);

final var path2 = Path.of("D:", "java-work", "AAA.TXT");
System.out.println(path2); // "D:\java-work\AAA.TXT"
System.out.println(path2.toRealPath()); // "D:\java-work\aaa.txt"

String toString ()

返回此路径的字符串表示形式。 (机器翻译)

final var path1 = Path.of("dir1", "dir2", "aaa.txt");
final var str1 = path1.toString();
System.out.println(str1); // "dir1\dir2\aaa.txt"

final var path2 = Path.of("D:");
final var str2 = path2.toString();
System.out.println(str2); // "D:"

final var path3 = Path.of("D:", "java-work", "aaa.txt");
final var str3 = path3.toString();
System.out.println(str3); // "D:\java-work\aaa.txt"

final var path4 = Path.of("aaa.txt");
final var str4 = path4.toString();
System.out.println(str4); // "aaa.txt"

URI toUri ()

返回一个 URI 来表示此路径。 (机器翻译)

final var userDir = System.getProperty("user.dir");
System.out.println(userDir); // "D:\java-work"

final var path1 = Path.of("aaa.txt");
System.out.println(path1); // "aaa.txt"
System.out.println(path1.toUri()); // "file:///D:/java-work/aaa.txt"

final var path2 = Path.of("dir1", "dir2", "aaa.txt");
System.out.println(path2); // "dir1\dir2\aaa.txt"
System.out.println(path2.toUri()); // "file:///D:/java-work/dir1/dir2/aaa.txt"

final var path3 = Path.of("D:");
System.out.println(path3); // "D:"
System.out.println(path3.toUri()); // "file:///D:/"

final var path4 = Path.of("D:", "xxx", "aaa.txt");
System.out.println(path4); // "D:\xxx\aaa.txt"
System.out.println(path4.toUri()); // "file:///D:/xxx/aaa.txt"

Methods declared in Iterable

forEach, spliterator

请参阅下面的链接。


相关文章

To top of page