Java 8 Streams: streaming files and delete after read

Viewed 1216

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:

  1. I create a Stream of Files
  2. I create a BufferedReader for each one of them
  3. I flatMap to the lines Stream of the BufferedReader
  4. I print each line.

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!

1 Answers

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.

Related