简体   繁体   English

如何使用 Streams 从目录中逐行读取所有文件?

[英]How can I read all files line by line from a directory by using Streams?

I have a directory called Files and it has many files.I want to read these Files line by line and store them an List<List<String>> .我有一个名为 Files 的目录,它有很多文件。我想逐行读取这些文件并将它们存储为List<List<String>>

./Files
 ../1.txt
 ../2.txt
 ../3.txt
 ..
 ..

it goes like that.事情就是这样。

private List<List<String>> records = new ArrayList<>();

List<Path> filesInFolder = Files.list(Paths.get("input"))
                .filter(Files::isRegularFile)
                .collect(Collectors.toList());

records = Files.lines(Paths.get("input/1.txt"))
                .map(row -> Arrays.asList(row.split(space)))
                .collect(Collectors.toList());

The logic basically is like逻辑基本上是这样的

List<List<String>> records = Files.list(Paths.get("input"))
    .filter(Files::isRegularFile)
    .flatMap(path -> Files.lines(path)
        .map(row -> Arrays.asList(row.split(" "))))
    .collect(Collectors.toList());

But you are required to catch the IOException potentially thrown by Files.lines .但是您需要捕获Files.lines可能引发的IOException Further, the stream returned by Files.list should be closed to release the associated resources as soon as possible.此外,Files.list 返回的Files.list应关闭以尽快释放相关资源。

List<List<String>> records; // don't pre-initialize
try(Stream<Path> files = Files.list(Paths.get("input"))) {
    records = files.filter(Files::isRegularFile)
        .flatMap(path -> {
            try {
                return Files.lines(path)
                    .map(row -> Arrays.asList(row.split(" ")));
            } catch (IOException ex) { throw new UncheckedIOException(ex); }
        })
        .collect(Collectors.toList());
}
catch(IOException|UncheckedIOException ex) {
    // log the error

    // and if you want a fall-back:
    records = Collections.emptyList();
}

Note that the streams returned by Files.lines used with flatMap are correctly closed automatically, as documented :请注意,与flatMap一起使用的Files.lines返回的流会自动正确关闭, 如文档所示:

Each mapped stream is closed after its contents have been placed into this stream.每个映射的 stream 在其内容已放入此 stream 后关闭。


It's also possible to move the map step from the inner stream to the outer:也可以将map步骤从内部 stream 移到外部:

List<List<String>> records; // don't pre-initialize
try(Stream<Path> files = Files.list(Paths.get("E:\\projects\\nbMJ\\src\\sub"))) {
    records = files.filter(Files::isRegularFile)
        .flatMap(path -> {
            try { return Files.lines(path); }
            catch (IOException ex) { throw new UncheckedIOException(ex); }
        })
        .map(row -> Arrays.asList(row.split(" ")))
        .collect(Collectors.toList());
}
catch(IOException|UncheckedIOException ex) {
    // log the error

    // and if you want a fall-back:
    records = Collections.emptyList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用扫描仪从命令行读取文件 - How can I read a file from the command line using scanner 如何从大字符串中逐行读取? - How can I read line after line from big String? 我如何使用apache-camel在一次轮询中从aws s3目录中读取所有文件 - How do i can read all the files from aws s3 directory in a single poll using apache-camel 使用写和读流Java的单行 - Single line using write and read of streams Java 我如何跳到特定行并在Java中从中读取 - How can I jump to specific line and read from that in java 使用 IntelliJ (cmd line template) 时,我在哪里可以找到资源文件的根目录? - When using IntelliJ (cmd line template) , where can I locate the root directory for resource files? 如何使用 Java 逐行读取大文本文件? - How can I read a large text file line by line using Java? 如何读取 Java 中的空行? - How can I read an empty line in Java? 如何从文本文件的下一行读取内容并暂停,以便稍后再从该行读取内容? - How can I read from the next line of a text file, and pause, allowing me to read from the line after that later? 如何使用MappedByteBuffer从java中的映射文件中逐行读取 - How to read line by line from mapped file in java using MappedByteBuffer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM