• Java8新特性一点通 | 回顾文件操作和watchService文件变更监视操作


    文件操作

    今天重温了一些文件操作:
    - 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);
        }
    }
  • 相关阅读:
    checkbox全选
    table隔行变色与table单元格根据条件更改字体颜色
    document.ready和window.onload
    JS实现定时弹出广告
    CSS overflow属性与display属性
    OpenCV 安装步骤
    C#类的继承多态(虚方法,隐藏方法、抽象类和抽象方法)
    C#中虚方法,抽象方法和隐藏方法
    C#的重载与重写
    C#中可空类型
  • 原文地址:https://www.cnblogs.com/evan-liang/p/12233928.html
Copyright © 2020-2023  润新知