【笔记】Java的Files
前言
Java的Files学习笔记
新建文件
1 | Path path = Files.createFile(Path.of(<file>)); |
新建目录
只新建一级目录
1 | Path path = Files.createDirectory(Path.of(<dir>)); |
递归新建多级目录
1 | Path path = Files.createDirectories(Path.of(<dir>)); |
新建硬链接
1 | Path path = Files.createLink(Path.of(<link>), Path.of(<file>)); |
删除文件或目录
1 | Files.delete(Path.of(<file>)); |
存在时才删除
1 | boolean success = Files.deleteIfExists(Path.of(<file>)); |
复制文件或目录
1 | Path path = Files.copy(Path.of(<file_source>), Path.of(<file_target>)); |
移动或重命名文件或目录
1 | Path path = Files.move(Path.of(<file_source>), Path.of(<file_target>)); |
获取文件系统
1 | FileStore fileStore = Files.getFileStore(Path.of(<file>)); |
判断是否是相同文件
1 | boolean yes = Files.isSameFile(Path.of(<file_1>), Path.of(<file_2>)); |
判断是否是隐藏文件
1 | boolean yes = Files.isHidden(Path.of(<file>)); |
获取Content-Type
1 | String contentType = Files.probeContentType(Path.of(<file>)); |
获取属主和属组
1 | String owner = Files.getOwner(Path.of(<file>)).name(); |
判断是否是软链接
1 | boolean yes = Files.isSymbolicLink(Path.of(<file>)); |
判断是否是目录
1 | boolean yes = Files.isDirectory(Path.of(<file>)); |
获取上次修改时间
1 | FileTime lastModifiedTime = Files.getLastModifiedTime(Path.of(<file>)); |
修改上次修改时间
<timestamp>:毫秒时间戳
1 | Path path = Files.setLastModifiedTime(Path.of(<file>), FileTime.fromMillis(<timestamp>)); |
获取文件大小
1 | long size = Files.size(Path.of(<file>)); |
判断文件是否存在
1 | boolean exist = Files.exists(Path.of(<file>)); |
判断文件是否不存在
1 | boolean notExist = Files.notExists(Path.of(<file>)); |
判断文件权限
判断是否可读
1 | boolean ok = Files.isReadable(Path.of(<file>)); |
判断是否可写
1 | boolean ok = Files.isWritable(Path.of(<file>)); |
判断是否可执行
1 | boolean ok = Files.isExecutable(Path.of(<file>)); |
读取文件
读取所有内容
1 | String result = Files.readString(Path.of(<file>)); |
指定编码
1 | String result = Files.readString(Path.of(<file>), StandardCharsets.UTF_8); |
读取行
1 | List<String> result = Files.readAllLines(Path.of(<file>)); |
指定编码
1 | List<String> result = Files.readAllLines(Path.of(<file>), StandardCharsets.UTF_8); |
写入文件
写入字节数组
覆盖写入
1 | Files.write(Path.of(<file>), "".getBytes()); |
追加写入
1 | Files.write(Path.of(<file>), "".getBytes(), StandardOpenOption.APPEND); |
写入字符串
覆盖写入
1 | Files.writeString(Path.of(<file>), ""); |
追加写入
1 | Files.writeString(Path.of(<file>), "", StandardOpenOption.APPEND); |
指定编码
1 | Files.writeString(Path.of(<file>), "", StandardCharsets.UTF_8); |
I/O流
获取BufferedReader
1 | BufferedReader bufferedReader = Files.newBufferedReader(Path.of(<file>)); |
获取BufferedWriter
1 | BufferedWriter bufferedWriter = Files.newBufferedWriter(Path.of(<file>)); |
从输入流读取数据
1 | long result = Files.copy(Files.newInputStream(Path.of(<file_source>)), Path.of(<file_target>)); |
数据写入到输出流
1 | long result = Files.copy(Path.of(<file_source>), Files.newOutputStream(Path.of(<file_target>))); |
获取目录下的所有文件和目录
- 必须传递目录,否则会报错
1 | Stream<Path> list = Files.list(Path.of(<dir>)); |
搜索文件
- Lambda表达式中返回true表示匹配成功,返回false表示匹配失败
2:递归深度
1 | Stream stream = Files.find(Path.of("<dir>"), 2, ((path, basicFileAttributes) -> { |
获取文件每一行内容
- 必须传递文件,否则会报错
1 | Stream<String> lines = Files.lines(Path.of(<file>)); |