提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
可以使用startsWith()和endsWith()方法完成部分路径比较
/** from * N o w J a v a . c o m - 时代Java **/ import java.nio.file.Path; import java.nio.file.Paths; public class Main { public static void main(String[] args) { Path path01 = Paths.get("/folder1/folder2/folder3/test.txt"); boolean sw = path01.startsWith("/folder1/folder2"); boolean ew = path01.endsWith("test.txt"); System.out.println(sw); System.out.println(ew); } } /* from nowjava - 时代Java */