05流与文件 动手动脑

使用Files.walkFileTree()找出指定文件夹下所有大于指定大小(比如1M)的文件

 

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;

@SuppressWarnings("rawtypes")
public class Files_walkFileTree implements FileVisitor {
    @SuppressWarnings("unused")
    private final PathMatcher matcher;
    private final long accepted_size;
    public Files_walkFileTree(String glob,long accepted_size) {
        matcher= FileSystems.getDefault().getPathMatcher("glob:" +glob);
        this.accepted_size=accepted_size; 
    }
    void search(Path file) throws IOException {
        long size = (Long) Files.getAttribute(file, "basic:size");
        if(size >=accepted_size) {
            System.out.println(file);
        }
    }
    public FileVisitResult postVisitDirectory(Object dir, IOException exc)throws IOException {
        return FileVisitResult.CONTINUE;
    }
    public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs)throws IOException {
        return FileVisitResult.CONTINUE;
    }
    public FileVisitResult visitFile(Object file, BasicFileAttributes attrs)throws IOException {
       search((Path) file);
       return  FileVisitResult.CONTINUE;
    }
    public FileVisitResult visitFileFailed(Object file, IOException exc)throws IOException {
       return FileVisitResult.CONTINUE;
    }
    
    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws IOException{
       String glob= "*.jpg";
       long size = 1048576;
       Path fileTree = Paths.get("D:/");
       Files_walkFileTree walk=new Files_walkFileTree(glob, size);
       EnumSet opts=EnumSet.of(FileVisitOption.FOLLOW_LINKS);
       System.out.println("D盘中大小等于1M的文件有");
       Files.walkFileTree(fileTree, opts, Integer.MAX_VALUE, walk);
    }
}
Files_walkFileTree.java

 

运行截图:

 

使用Files.walkFileTree()找出指定文件夹下所有扩展名为.txt或.java的文件

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class Files_walkFileTree {
    public static void main(String args[]) throws IOException {
        String glob = "glob:**/*.{java,txt}";
        String path = "D:/";
        match(glob, path);
    }

    public static void match(String glob, String location) throws IOException {

        final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher( glob);

        Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path path,
                    BasicFileAttributes attrs) throws IOException {
                if (pathMatcher.matches(path)) {
                    System.out.println(path);
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc)
                    throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
    }

}
Files_walkFileTree.java

运行截图:

 

 

使用FIle.walkFileTree()找出指定文件夹下所有包容指定字符串的txt文件

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class Files_walkFileTree {
    public static void main(String args[]) throws IOException {
        String glob = "glob:**/*.txt";
        String path = "D:/eclipse_workspace/Files";
        match(glob, path);
    }

    public static void match(String glob, String location) throws IOException {
        final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher( glob);
        Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path path,
                 BasicFileAttributes attrs) throws IOException {
                     if (pathMatcher.matches(path)) {
                         BufferedReader reader =Files.newBufferedReader(path);
                         String line=null;
                         while((line = reader.readLine()) !=null) {
                             if(line=="abcd") 
                                 System.out.println(path);
                                 break;
                             }
                         }
                     }
                    return FileVisitResult.CONTINUE;
                }
                @Override
                public FileVisitResult visitFileFailed(Path file, IOException exc)
                    throws IOException {
                    return FileVisitResult.CONTINUE;
                }
            });
        }

}
Files_workFileTree.java

 

Path类实现了Watchable接口,因此我们能监控它的变化。示例FileWatchDemo.java展示了如何监控一个文件夹中文件新增、删除和改名。请通过查询JDK文件和使用搜索引擎等方式,看懂此示例代码,并弄明白Watchable、WatchService等类型之间的关系,使用UML类图表示出这些类之间的关系。

java.nio.file.WatchService文件系统监视服务的接口类,它的具体实现由监视服务提供者负责加载。

java.nio.file.Watchable 实现了 java.nio.file.Watchable 的对象才能注册监视服务 WatchService。java.nio.file.Path实现了 watchable 接口,后文使用 Path 对象注册监视服务。

 java.nio.file.WatchKey 该类代表着 Watchable 对象和监视服务 WatchService 的注册关系。WatchKey 在 Watchable 对象向 WatchService 注册的时候被创建。它是 Watchable 和 WatchService 之间的关联类。

 

转载于:https://www.cnblogs.com/gothic-death/p/9997822.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值