简体   繁体   中英

Write a Stream<String> to a file Java

I'm reading a file using the java NIO package's Files.lines() method, which gives an output of type Stream<String> . After some manipulation to the String records I want to write it to a file. I've tried collecting it to a list using Collectors.toList() , and it works for smaller data set. The problem occurs when my file has almost 1 million lines(records), the list isn't able to hold as many records.

// 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);

I'm looking for a way I can read a large file, manipulate it (as done in the .map() method in my example), and write it into a file without storing it into a List(or collection).

You can try this (update the code to close the resources):

    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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM