Path
dk7 引入了 Path 和 Paths 类
- Path 用来表示文件路径
- Paths 是工具类,用来获取 Path 实例
java Path source = Paths.get("1.txt"); // 相对路径 使用 user.dir 环境变量来定位 1.txt
Path source = Paths.get("d:\1.txt"); // 绝对路径 代表了 d:\1.txt
Path source = Paths.get("d:/1.txt"); // 绝对路径 同样代表了 d:\1.txt
Path projects = Paths.get("d:\data", "projects"); // 代表了 d:\data\projects
.
代表了当前路径..
代表了上一级路径
Files
检查文件是否存在
java Path path = Paths.get("helloword/data.txt"); System.out.println(Files.exists(path));
创建一级目录
java Path path = Paths.get("helloword/d1");
Files.createDirectory(path);
- 如果目录已存在,会抛异常 FileAlreadyExistsException
- 不能一次创建多级目录,否则会抛异常 NoSuchFileException
创建多级目录用
java Path path = Paths.get("helloword/d1/d2");
Files.createDirectories(path);
拷贝文件
```java Path source = Paths.get("helloword/data.txt"); Path target = Paths.get("helloword/target.txt