Java8新特性一点通 | 回顾文件操作和watchService文件变更监视操作
2024-10-08 05:10:52
文件操作
今天重温了一些文件操作:
- Files.list()
遍历文件和目录
//List all files and sub-directories using Files.list()
try {
Files.list(Paths.get(".")).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
Files.newDirectoryStream()
遍历文件和目录
//List files and sub-directories with Files.newDirectoryStream()
try {
Files.newDirectoryStream(Paths.get(".")).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
Files::isReularFile
找出目录中的文件
//List only files inside directory using filter expression
try {
Files.list(Paths.get(".")).filter(Files::isRegularFile).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
file->file.isHidden()
找出隐藏文件
//Find all hidden files in directory
final File[] files = new File(".").listFiles(file->file.isHidden());
for(File file:files){
System.out.println(file.getName());
}
Files.newBufferedWriter
迅速创建一个BufferedWriter,可以使编码语法更简洁
//Write to file using BufferedWriter
Path path = Paths.get("D:\\test.txt");
try(BufferedWriter writer = Files.newBufferedWriter(path)){
writer.write("Hello World!");
} catch (IOException e) {
e.printStackTrace();
}
Files.write()
使用简介的语法写入内容到文件
//Write to file using Files.write()
try {
Files.write(Paths.get("D:\\test1.txt"),"Hello".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
Tips:
对于比较大文件夹,使用DirectoryStream
会性能更好
WatchService
利用WatchService
对文件目录进行监视,可以对目录中增删改查这些动作进行监控
public class WatchServiceExample2 {
public static void main(String[] args) throws IOException {
Path curPath = Paths.get(".");
WatchService watchService = curPath.getFileSystem().newWatchService();
//遍历并注册目录
walkAndRegisterDirectories(curPath, watchService);
try {
//监听目录变化
while (true) {
WatchKey watchKey = watchService.take();
for (WatchEvent event : watchKey.pollEvents()) {
System.out.println(event.kind());
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void walkAndRegisterDirectories(Path path, WatchService watchService) throws IOException {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
registerDirectory(dir, watchService);
return FileVisitResult.CONTINUE;
}
});
}
private static void registerDirectory(Path dir, WatchService watchService) throws IOException {
WatchKey key = dir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE
, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
}
}
最新文章
- 基于STM32Cube的脉冲输出
- 文件上传之——用SWF插件实现文件异步上传和头像截取
- 基于命令行编译打包phonegap for android应用 分类: Android Phonegap 2015-05-10 10:33 73人阅读 评论(0) 收藏
- FreeRTOS和Ucos在打开关闭中断的区别
- codeforces 659 G. Fence Divercity 组合数学 dp
- 妙味课堂——HTML+CSS(第四课)(一)
- PHP发送微信模版消息
- MAC虚拟机NAT方式共享上网设置
- 对象创建型模式------Builder(生成器或建造者模式)(2)
- poj 1011 搜索减枝
- Java SpringMVC小白的成长(一)
- 嵌套查询别名必须性示例。HAVING用法
- SQL外连接
- jdk1.7中的常量池
- Hall定理 二分图完美匹配
- POJ 3140.Contestants Division 基础树形dp
- C# 之 4个访问修饰符和8个声明修饰符详解
- js高阶函数map和reduce
- java web项目为什么我们要放弃jsp?
- KVM虚拟化管理 virt manager常用操作