I want to stream the lines contained in files but deleting each file once it has been processed.
The current process is like this:
Explanation:
Code:
(1) Stream.generate(localFileProvider::getNextFile)
(2) .map(file -> return new BufferedReader(new InputStreamReader(new FileInputStream(file))))
(3) .flatMap(BufferedReader::lines)
(4) .map(System.out::println);
Would it be possible to delete each file once it has been completely read and continue processing the other files in the stream?
Thank you!
When you use flatMap, the stream returned by the function will get closed automatically, once it has been processed. You only need to add an option when opening the InputStream, specifying that its underlying file should get deleted on close.
Assuming that localFileProvider.getNextFile() returns a java.io.File, the code looks like
Stream.generate(localFileProvider::getNextFile)
.takeWhile(Objects::nonNull) // stop on null, otherwise, it’s an infinite stream
// the actual operation regarding your question:
.flatMap(file -> {
try { return new BufferedReader(new InputStreamReader(
Files.newInputStream(file.toPath(), StandardOpenOption.DELETE_ON_CLOSE)))
.lines();
} catch(IOException ex) { throw new UncheckedIOException(ex); }
})
// the example terminal operation
.forEach(System.out::println);
You need to use Files.newInputStream(Path, OpenOption...) instead of new FileInputStream(…) to specify the special open option. So the code above converts the File to a Path via toPath(); if getNextFile() returns a String, you would need Paths.get(file) instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With