I'm reading a large file using the nio Files.lines method, and writing it to another file.
BufferedWriter writer = Files.newBufferedWriter(Path.of(outFile);
Files.lines(Path.of(inputFile))
.forEach(line -> {
try {
writer.write(line);
writer.newLine();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
writer.flush();
writer.close();
I want to close the writer and the stream (Files.lines) in a finally block.
I'm aware I'll have to surround this snippet in a try-catch-finally block, but how do I close the stream without assigning it to a variable?