Warm tip: This article is reproduced from serverfault.com, please click

collections-将Stream 写入文件Java

(collections - Write a Stream to a file Java)

发布于 2020-12-02 16:17:46

我正在使用java NIO包的Files.lines()方法读取文件,该文件给出type的输出Stream<String>在对字符串记录进行一些操作之后,我想将其写入文件。我尝试使用将其收集到列表中Collectors.toList(),并且适用于较小的数据集。当我的文件有将近一百万行(记录)时,就会出现问题,该列表无法容纳那么多的记录。

// Read the file using Files.lines and collect it into a List
        List<String> stringList = Files.lines(Paths.get("<inputFilePath>"))
                                    .map(line -> line.trim().replaceAll("aa","bb"))
                                    .collect(Collectors.toList());


//  Writes the list into the output file
        Files.write(Paths.get("<outputFilePath>"), stringList);

我正在寻找一种读取大文件,对其进行操作(如本.map()中的方法中所述)并将其写入文件而不将其存储到List(或集合)中的方法。

Questioner
Abhinandan Madaan
Viewed
11
Octavian R. 2020-12-03 02:24:25

你可以尝试这样做(更新代码以关闭资源):

    try (BufferedWriter writer = Files.newBufferedWriter(Path.of(outFile), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
         Stream<String> lines = Files.lines(Path.of(inFile))) {
        // Read the file using Files.lines and collect it into a List
        lines.map(line -> line.trim().replaceAll("aa", "bb"))
                .forEach(line -> {
                    try {
                        writer.write(line);
                        writer.newLine();
                    } catch (IOException e) {
                        throw new UncheckedIOException(e);
                    }
                });
        writer.flush();
    }