java8--IO工具类(java疯狂讲义3复习笔记)

Paths类

public static void pathTest(){
        Path path = Paths.get("~");
        System.out.println(path);
        System.out.println(path.getNameCount());
        System.out.println(path.getRoot());
        Path absolutePath = path.toAbsolutePath();
        System.out.println(absolutePath);
        System.out.println(absolutePath.getNameCount());
        System.out.println(absolutePath.getFileSystem());
        //依次打印父路径
        absolutePath.forEach(name->System.out.println(name));
    }

Files类 ,这个是重点

public static void filesTest() throws FileNotFoundException, IOException{
        //复制文件
        Files.copy(Paths.get("abc.txt"), new FileOutputStream("testFile/abc2.txt"));
        //判断是否隐藏文件
        System.out.println(Files.isHidden(Paths.get("abc.txt")));
        //读取文件的所有行到list
        List<String> lines = Files.readAllLines(Paths.get("abc.txt"));
        lines.forEach (line -> System.out.println(line));
        //文件大小
        System.out.println(Files.size(Paths.get("abc.txt")));
        //向文件里写入东西
        List<String> poem = new ArrayList<String>();
        poem.add("quzhinannnnnnnnnnn曲志楠南安安那那那那那");
        poem.addAll(lines);
        Files.write(Paths.get("testFile/path.txt"), poem, Charset.forName("utf-8"));
        //列出文件目录下的所有文件(不递归)
        Files.list(Paths.get("/")).forEach(Name -> System.out.println(Name));
        //打印所有文件到控制台
        Files.lines(Paths.get("abc.txt"),Charset.forName("utf-8")).forEach(line -> System.out.println(line));
        //查看存储空间
        FileStore fs = Files.getFileStore(Paths.get("/"));
        System.out.println(fs.getTotalSpace());
    }

FileVisitor遍历文件和目录

 

public static void walkFileTree() throws IOException{
        Files.walkFileTree(Paths.get("."), new SimpleFileVisitor<Path>() {  
              
            @Override  
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {  
                // TODO Auto-generated method stub  
                // return super.preVisitDirectory(dir, attrs);  
                System.out.println("正在访问:" + dir + "目录");  
                return FileVisitResult.CONTINUE;  
            }  
  
            @Override  
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {  
                // TODO Auto-generated method stub  
                // return super.visitFile(file, attrs);  
                System.out.println("\t正在访问" + file + "文件");  
                if (file.endsWith("LearnIO.java")) {  
                    System.out.println("******找到目标文件LearnIO.java******");  
                    return FileVisitResult.TERMINATE; // 找到了就终止  
                }
                return FileVisitResult.CONTINUE; // 没找到继续找  
            }  
        });  ;
    }

 使用WatchService监控文件变化

private static void WatchServiceTest() throws IOException, InterruptedException {
        // TODO Auto-generated method stub
        WatchService watchService = FileSystems.getDefault().newWatchService();
        Paths.get(".").register(watchService,
                StandardWatchEventKinds.ENTRY_CREATE,
                StandardWatchEventKinds.ENTRY_DELETE,
                StandardWatchEventKinds.ENTRY_MODIFY
                );
        while(true){
            //获取下一个文件的变化事件
            WatchKey key = watchService.take();
            for(WatchEvent<?> event : key.pollEvents()){
                System.out.println(event.context()+"文件发生了"
                        +event.kind()+"事件");
            }
            //重设WatchKey
            boolean valid = key.reset();
            //如果重设失败,退出监听
            if(!valid){
                break;
            }
        }
    }

访问文件属性,这个感觉用的不多,用到时再来看吧.

 

posted @ 2017-01-18 11:35  lakeslove  阅读(1124)  评论(0编辑  收藏  举报