Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 8:列出来自多个路径的文件_Java_File_Java 8 - Fatal编程技术网

Java 8:列出来自多个路径的文件

Java 8:列出来自多个路径的文件,java,file,java-8,Java,File,Java 8,如何在Java8中从多个路径搜索文件。这些不是子/同级目录。例如,如果我想在路径中搜索json文件,我有: try (Stream<Path> stream = Files.find(Paths.get(path), Integer.MAX_VALUE, (p, attrs) -> attrs.isRegularFile() && p.toString().endsWith(".json"))) { stream.map((p) -> p.name).

如何在Java8中从多个路径搜索文件。这些不是子/同级目录。例如,如果我想在路径中搜索json文件,我有:

try (Stream<Path> stream = Files.find(Paths.get(path), Integer.MAX_VALUE, (p, attrs) -> attrs.isRegularFile() && p.toString().endsWith(".json"))) {
  stream.map((p) -> p.name).forEach(System.out::println);
}
try(Stream=Files.find(path.get(path),Integer.MAX_值,(p,attrs)->attrs.isRegularFile()&&p.toString().endsWith(“.json”)){
stream.map((p)->p.name).forEach(System.out::println);
}

有没有更好的方法在多个路径中搜索?或者我必须为多个路径运行相同的代码吗?

是的,您可以这样做。假设您有路径作为
对象的
列表
,您可以这样做

List<String> paths = ...;

paths.stream().map(path -> {
    try (Stream<Path> stream = Files.list(Paths.get(path))) {
        return stream.filter(p -> !p.toFile().isDirectory()).filter(p -> p.toString().endsWith(".json"))
                .map(Path::toString).collect(Collectors.joining("\n"));
    } catch (IOException e) {
        // Log your ERROR here.
        e.printStackTrace();
    }
    return "";
}).forEach(System.out::println);
列表路径=。。。;
path.stream().map(路径->{
try(Stream=Files.list(path.get(path))){
返回stream.filter(p->!p.toFile().isDirectory()).filter(p->p.toString().endsWith(“.json”))
.map(路径::toString.collect(收集器.joining(“\n”));
}捕获(IOE异常){
//在这里记录您的错误。
e、 printStackTrace();
}
返回“”;
}).forEach(System.out::println);
如果您需要删除新行字符,那么也可以这样做

paths.stream().map(path -> {
    try (Stream<Path> stream = Files.walk(Paths.get(path))) {
        return stream.filter(p -> !p.toFile().isDirectory()).filter(p -> p.toString().endsWith(".json"))
                .map(Path::toString).collect(Collectors.toList());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Collections.emptyList();
}).flatMap(List::stream).forEach(System.out::println);
path.stream().map(路径->{
try(Stream=Files.walk(path.get(path))){
返回stream.filter(p->!p.toFile().isDirectory()).filter(p->p.toString().endsWith(“.json”))
.map(Path::toString).collect(Collectors.toList());
}捕获(IOE异常){
e、 printStackTrace();
}
返回集合。emptyList();
}).flatMap(List::stream).forEach(System.out::println);

在这里,您可以将每个路径的所有
.json
文件名放入
列表
,然后在打印之前将它们平铺成
字符串
对象的平坦
流。请注意,此方法中涉及的附加步骤是
flatMap

,这就是
flatMap
的用途

如果在
Path
中有
Path
实例的集合,则可以使用

paths.stream()
     .flatMap(path -> {
         try { return Files.find(path, Integer.MAX_VALUE,
            (p, attrs) -> attrs.isRegularFile() && p.toString().endsWith(".json")); }
         catch (IOException ex) { throw new UncheckedIOException(ex); }
     })
     .forEach(System.out::println);
这有点笨拙,因为我们必须在这里处理由
find
声明的选中的
IOException
。将其作为
UncheckedIOException
重新排序是最简单的选择,因为如果在处理流时发生I/O问题,则
find
返回的流无论如何都会这样做。比照

如果从该方法返回后在访问目录时抛出,则会将其包装在一个中,该中会从导致访问发生的方法中抛出

因此,在我们的函数中执行同样的操作简化了调用方的错误处理,因为它只需要处理
UncheckedIOException
s

在这里没有办法使用
try(…)
,但这正是
flatMap
将从我们身上消除这个负担的原因。如各国所述:

每个映射流都是在其内容被放入该流之后进行的

因此,一旦我们的函数返回了子流,流实现将为我们做正确的事情

您可以链接任意流操作来代替
.forEach(System.out::println)
以像处理单个流一样处理平坦流的所有元素

如果输入集合包含
String
而不是
Path
,则只需将
paths.stream().map(paths::get)
而不是
paths.stream()
前置到
flatMap
操作