Path
Path是一个接口,表示一个目录名序列,还可以表示一个文件。
创建Path的方法如下:
1 | //Paths类的get方法 |
例如:
1 | Path path = Paths.get("D:/", "abc"); |
Path的常用方法
1 | Path resolve(Path other) |
示例如下:
1 | public class Main { |
Files
Files可以让我们很简单地处理常用的文件操作。
读写文件
1 | static byte[] readAllBytes(Path path) |
关于OpenOption,下面会介绍。
创建文件和目录
1 | static Path createFile(Path path, FileAttribute<?>... attrs) |
复制,移动和删除文件
1 | static Path Copy(Path from,Path to,CopyOption...op) |
用于操作文件的标准选项


获取文件信息
1 | static boolean exists(Path path) |
可以通过readAttributes方法获取BasicFileAttributes接口,这个接口储存了文件的基本信息,例如:
1 | BasicFileAttributes attributes = Files.readAttributes(Paths.get("/abc/a"),BasicFileAttributes.class); |
BasicFileAttributes的方法有
1 | FileTime creationTime() |