带大小筛选器的Java8全局路径匹配器

带大小筛选器的Java8全局路径匹配器,java,Java,我的目标是创建一个具有globs功能的FileScanner,并添加一些自定义过滤器函数(如min或max filesize) 我的第一次尝试是: final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:**/*.java"); Path path2 = Paths.get("c:/dummy"); try (final Stream<Path> stream

我的目标是创建一个具有globs功能的FileScanner,并添加一些自定义过滤器函数(如min或max filesize)

我的第一次尝试是:

    final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:**/*.java");
    Path path2 = Paths.get("c:/dummy");
    try (final Stream<Path> stream = Files.list(path2)) {
        stream.filter(pathMatcher::matches).forEach(FileProcessor::processFile);
    }
final-PathMatcher-PathMatcher=FileSystems.getDefault().getPathMatcher(“glob:*/*.java”);
Path path2=Path.get(“c:/dummy”);
try(最终流=Files.list(路径2)){
stream.filter(pathMatcher::matches).forEach(FileProcessor::processFile);
}
为了在c:\dummy目录中只找到*.java文件,这项工作做得很好。但是为了只查找小于1000字节的*.java文件,我需要做什么

我的尝试是:

    PathMatcher myMatcher = path -> {
        // Do size comparision ...
        return true;
    };

    Path path = Paths.get("c:/dummy");
    try (final Stream<Path> stream = Files.list(path)) {
        stream.filter(myMatcher::matches).forEach(FileProcessor::processFile);
    }
PathMatcher-myMatcher=path->{
//做尺寸比较。。。
返回true;
};
Path Path=Path.get(“c:/dummy”);
try(最终流=Files.list(路径)){
stream.filter(myMatcher::matches).forEach(FileProcessor::processFile);
}
但有了这个解决方案,我再也没有地球仪了

最后一次尝试是:

    final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:**/*.java");
    Path path2 = Paths.get("c:/dummy");
    try (final Stream<Path> stream = Files.list(path2)) {
        stream.filter(pathMatcher::matches).forEach(FileProcessor::processFile);
    }
Path path3=Path.get(“c:/dummy”); final PathMatcher pathMatcher2=FileSystems.getDefault().getPathMatcher(“glob:*/*.java”)

Files.walkFileTree(路径3,新的SimpleFileVisitor(){
@凌驾
公共文件VisitResult visitFile(路径路径,基本文件属性属性属性)引发IOException{
if(pathMatcher2.matches(path)){
//做尺寸比较
}
返回FileVisitResult.CONTINUE;
}
@凌驾
公共文件VisitResult visitFileFailed(路径文件,IOException exc)引发IOException{
返回FileVisitResult.CONTINUE;
}
});
最后一个有效,但它看起来不太好,而且使用SimpleFileVisitor对象感觉有点复杂

有人知道如何解决这个问题吗

您好,
Hauke

根据要检查的文件名的属性,您可以使用
文件。find()

// Note that you can use a `PathMatcher` for the name instead if you want
final BiPredicate<Path, BasicFileAttributes> filter =
    (path, attrs) -> path.getFileName().toString().endsWith(".java") && attrs.size() <= 1000L;

try (
    final Stream<Path> = Files.find(baseDir, Integer.MAX_VALUE, filter);
) {
    // process the stream
}
//请注意,如果需要,可以使用'PathMatcher'作为名称
最终双预测滤波器=

(path,attrs)->path.getFileName().toString().endsWith(“.java”)&&attrs.size()
Files.list()
将不会深入…小更正:您必须在“getFileName()”之后调用“toString”,因为getFileName返回一个path对象,然后endsWith具有不同的meaning@Hauke哎呀。。。是的,我总是忘记那个