java.nio.file, n now means "non-blocking" .
Path objects can easily yield parts of their path:
// files/PartsOfPaths.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.nio.file.*;
public class PartsOfPaths {
public static void main(String[] args) {
System.out.println(System.getProperty("os.name"));
Path p = Paths.get("PartsOfPaths.java").toAbsolutePath();
System.out.println("p:" + p);
for (int i = 0; i < p.getNameCount(); i++) {
System.out.println(p.getName(i));
}
System.out.println("ends with '.java': " + p.endsWith(".java")); // false. [1]
// p.endsWith("PartsOfPaths.java") produce true
for (Path pp : p) {
System.out.print(pp + ": ");
System.out.print(p.startsWith(pp) + " : ");
System.out.println(p.endsWith(pp));
}
System.out.println("Starts with " + p.getRoot() + " " + p.startsWith(p.getRoot()));
}
}
/* My Output:
Mac OS X
p:/Users/wangbingfeng/github/OnJava8-Examples/files/PartsOfPaths.java
Users
wangbingfeng
github
OnJava8-Examples
files
PartsOfPaths.java
ends with '.java': false
Users: false : false
wangbingfeng: false : false
github: false : false
OnJava8-Examples: false : false
files: false : false
PartsOfPaths.java: false : true
Starts with / true
*/
Note that[1], even though my path here does end with .java, endsWith() produces false. This is because endsWith() is comparing the entire path component, not a substring within the name.
-
endsWith
Tests if this path ends with the given path.boolean endsWith(Path other)If the given path has N elements, and no root component, and this path has N or more elements, then this path ends with the given path if the last N elements of each path, starting at the element farthest from the root, are equal.
If the given path has a root component then this path ends with the given path if the root component of this path ends with the root component of the given path, and the corresponding elements of both paths are equal. Whether or not the root component of this path ends with the root component of the given path is file system specific. If this path does not have a root component and the given path has a root component then this path does not end with the given path.
If the given path is associated with a different
FileSystemto this path thenfalseis returned.Parameters:
other- the given pathReturns:
trueif this path ends with the given path; otherwisefalse
-
endsWith
boolean endsWith(String other)Tests if this path ends with a
Path, constructed by converting the given path string, in exactly the manner specified by theendsWith(Path)method. On UNIX for example, the path "foo/bar" ends with "foo/bar" and "bar". It does not end with "r" or "/bar". Note that trailing separators are not taken into account, and so invoking this method on thePath"foo/bar" with theString"bar/" returnstrue.Parameters:
other- the given path stringReturns:
trueif this path ends with the given path; otherwisefalseThrows:
InvalidPathException- If the path string cannot be converted to a Path.
references:
1. On Java 8 - Bruce Eckel
2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/files/PartsOfPaths.java
3. https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html#endsWith-java.lang.String-
本文探讨了Java NIO中Path类的endsWith方法的工作原理,通过实例展示了如何判断路径是否以特定子路径结束,解释了该方法在不同情况下的行为。
2931

被折叠的 条评论
为什么被折叠?



