1 需求
2 接口
Class Paths
java.lang.Object
java.nio.file.Paths
- Method Detail
- public static Path get(String first, String... more)
- public static Path get(URI uri)
3 示例代码
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathDemo {
public static void main(String[] args) {
// 创建Path对象 方法一
Path path1 = Path.of("C:\test.txt");
// 创建Path对象 方法二
Path path2 = Paths.get("C:\test.txt");
// 获取父路径
System.out.println("getParent: " + path1.getParent());
// 获取文件名
System.out.println("getFileName: " + path1.getFileName());
// 将Path对象转化为File对象
File file = path1.toFile();
}
}
4 参考资料



