文件操作

今天重温了一些文件操作:

- 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);
}
}

最新文章

  1. 基于STM32Cube的脉冲输出
  2. 文件上传之——用SWF插件实现文件异步上传和头像截取
  3. 基于命令行编译打包phonegap for android应用 分类: Android Phonegap 2015-05-10 10:33 73人阅读 评论(0) 收藏
  4. FreeRTOS和Ucos在打开关闭中断的区别
  5. codeforces 659 G. Fence Divercity 组合数学 dp
  6. 妙味课堂——HTML+CSS(第四课)(一)
  7. PHP发送微信模版消息
  8. MAC虚拟机NAT方式共享上网设置
  9. 对象创建型模式------Builder(生成器或建造者模式)(2)
  10. poj 1011 搜索减枝
  11. Java SpringMVC小白的成长(一)
  12. 嵌套查询别名必须性示例。HAVING用法
  13. SQL外连接
  14. jdk1.7中的常量池
  15. Hall定理 二分图完美匹配
  16. POJ 3140.Contestants Division 基础树形dp
  17. C# 之 4个访问修饰符和8个声明修饰符详解
  18. js高阶函数map和reduce
  19. java web项目为什么我们要放弃jsp?
  20. KVM虚拟化管理 virt manager常用操作

热门文章

  1. 《面试宝典》 2019年springboot面试高频题(java)
  2. 【TCP/IP网络编程】:09套接字的多种可选项
  3. 「洛谷P1198」 [JSOI2008]最大数 解题报告
  4. Codeforces Round #519 by Botan Investments(前五题题解)
  5. C Primer Plus(二)
  6. Linux 踩坑记
  7. 【记】VM VirtualBox 网络地址转换(NAT)使用详解
  8. 【JavaScript学习笔记】函数、数组、日期
  9. 如何应用threejs实现立方体每个面用图片替换
  10. Java设计模式之三种工厂模式