java中的Path和Files之Path
Path是一个目录名序列,后面可以跟一个文件名。路径中的第一个参数可以是根部件,如:/ 或者C:\ , 而允许访问的根部件取决于文件系统。以根部件开始的路径是绝对路径,反之则是相对路径。
获得 Path对象
1.通过 Paths.get 直接传入 String 字符串方法获得
例如:
public class Test {
public static void main(String[] args) {
Path absolutePath = Paths.get("/test1","test2");
Path relativePath = Paths.get("test1", "test2","test3","test.txt");
System.out.println("absolutePath: " + absolutePath);
System.out.println("relativePath: " + relativePath);
}
}
控制台输出:
absolutePath: /test1/test2
relativePath: test1/test2/test3/test.txt
静态的 Paths.get 方法接收一个或者多个字符串,并将他们用默认文件系统的路径分隔符(类 Unix 文件系统是 / ,Windows 系统是 \ )连接起来,然后解析连接起来的结果,如果表示的不是给定系统中的合法路径,就会抛出 InvalidPathException 异常,这个连接起来的结果就是一个 Path 对象(结果仅仅表示一个路径,该文件或者该文件夹并不一定真实存在)。
2.通过读取配置文件获得
- test.properties
test.dir=test
- 代码demo
public class Test {
public static void main(String[] args) throws FileNotFoundException, IOException {
Path relativePath = Paths.get(System.getProperty("user.dir"),"src","test.properties");
Properties properties = new Properties();
properties.load(new FileReader(relativePath.toFile()));
String dir = properties.getProperty("test.dir");
Path path = Paths.get(dir);
System.out.println("path: " + path);
}
}
- 控制台输出:
path: test
Path 常用方法
- static Path get(String first, String… more)
通过连接给定的字符串创建一个路径
public class Test {
public static void main(String[] args) {
/*
* static Path get(String first, String... more)
* 通过连接给定的字符串创建一个路径
*/
Path absolutePath = Paths.get("/test1","test2");
Path relativePath = Paths.get("root","test1", "test2","test3","test.txt");
System.out.println("absolutePath: " + absolutePath);
System.out.println("relativePath: " + relativePath);
}
}
控制台输出:
absolutePath: /test1/test2
relativePath: root/test1/test2/test3/test.txt
- Path resolve(Path other)
Path resolve(String other)
若 other 是绝对路径,那么就返回 other ;否则,返回通过连接 this 和 other 获得的路径。
public class Test {
public static void main(String[] args) {
Path absolutePath = Paths.get("/test1","test2");
Path relativePath = Paths.get("root","test1", "test2","test3","test.txt");
/*
* Path resolve(Path other)
* Path resolve(String other)
* 若 other 是绝对路径,那么就返回 other ;否则,返回通过连接 this 和 other 获得的
* 路径。
*/
Path p1 = absolutePath.resolve(relativePath);
// other 是相对路径,返回absolutePath + relativePath
System.out.println("resolve: " + p1);
Path p2 = absolutePath.resolve(absolutePath);
// other 是绝对路径直接返回 absolutePath
System.out.println("resolve :" + p2);
}
}
控制台输出:
resolve: /test1/test2/root/test1/test2/test3/test.txt
resolve :/test1/test2
- Path resolveSibling(Path other)
Path resolveSibling(String other)
若 other 是绝对路径, 那么就返回 other ;否则,返回通过连接 this 的父路径和 other 获得的路径
public class Test {
public static void main(String[] args) {
Path absolutePath = Paths.get("/test1","test2");
Path relativePath = Paths.get("root","test1", "test2","test3","test.txt");
/*
* Path resolveSibling(Path other)
* Path resolveSibling(String other)
* 若 other 是绝对路径, 那么就返回 other ;否则,返回通过连接 this 的
* 父路径和 other 获得的路径
*/
Path p3 = absolutePath.resolveSibling(absolutePath);
// other 是绝对路径,返回 absolutePath
System.out.println("resolveSibling: " +p3);
// other 是相对路径, 返回 relativePath 的父路径 和 relativePath
Path p4 = absolutePath.resolveSibling(relativePath);
System.out.println("resolveSibling: " + p4);
}
}
控制台输出:
resolveSibling: /test1/test2
resolveSibling: /test1/root/test1/test2/test3/test.txt
- Path relativize(Path other)
返回用 this 进行解析,相对于 other 的相对路径.
public class Test {
public static void main(String[] args) {
Path absolutePath = Paths.get("/test1","test2");
Path relativePath = Paths.get("root","test1", "test2","test3","test.txt");
/*
* Path relativize(Path other)
* 返回用 this 进行解析,相对于 other 的相对路径
*/
Path relativePath2 = Paths.get("root","test1", "test2","test4","test2.txt");
Path p5 = relativePath.relativize(relativePath2);
System.out.println("relativize: " +p5);
}
}
控制台输出:
relativize: ../../test4/test2.txt
- Path normalize()
移除如 . 或者 … 等冗余的路径元素
public class Test {
public static void main(String[] args) {
/*
* Path normalize()
* 移除如 . 或者 .. 等冗余的路径元素
*/
Path p6 = Paths.get("test",".","test","..","test.txt");
Path p7 = p6.normalize();
System.out.println("normalize: " + p7);
}
}
控制台输出:
normalize: test/test.txt
- Path toAbsolutePath()
返回与该路径等价的绝对路径
public class Test {
public static void main(String[] args) {
Path relativePath = Paths.get("root","test1", "test2","test3","test.txt");
/*
* Path toAbsolutePath()
* 返回与该路径等价的绝对路径
*/
Path p8 = relativePath.toAbsolutePath();
System.out.println("toAbsolutePath: " + p8);
}
}
控制台输出:
toAbsolutePath: /Users/zhangzheng/eclipse-workspace/test/root/test1/test2/test3/test.txt
- Path getParent()
返回父路径,或者没有父路径时返回null
public class Test {
public static void main(String[] args) {
Path absolutePath = Paths.get("/test1","test2");
/*
* Path getParent()
* 返回父路径,或者没有父路径时返回null
*/
Path p9 = absolutePath.getParent();
System.out.println("getParent: " + p9);
}
}
控制台输出:
getParent: /test1
- Path getFileName();
返回该路径的最后一个部件,或者在该路径没有任何部件时返回,返回null
public class Test {
public static void main(String[] args) {
Path relativePath = Paths.get("root","test1", "test2","test3","test.txt");
/*
* Path getFileName();
* 返回该路径的最后一个部件,或者在该路径没有任何部件时返回,返回null
*/
Path p10 = relativePath.getFileName();
System.out.println("getFileName: " + p10);
}
}
控制台输出:
getFileName: test.txt
- Path getRoot()
返回该路径的根
public class Test {
public static void main(String[] args) {
Path absolutePath = Paths.get("/test1","test2");
/*
* Path getRoot()
* 返回该路径的根
*/
Path p11 = absolutePath.getRoot();
System.out.println("getRoot: " + p11);
}
}
控制台输出:
getRoot: /
- FIle toFile()
获得一个file 对象
public class Test {
public static void main(String[] args) {
Path relativePath = Paths.get("root","test1", "test2","test3","test.txt");
/*
* FIle toFile()
* 获得一个file 对象
*/
File file = relativePath.toFile();
System.out.println("toFile: " + file);
}
}
控制台输出:
toFile: root/test1/test2/test3/test.txt