Let's say I want to perform the following operations:
- List the files in a given directory (as a stream)
- Map each file (Path) into a Reader (BufferedReader for example) for a consumer to consume.
- Once a file has been consumed, delete the files
The code would look a bit like this:
Stream<Reader> stream = Files.list(Paths.get("myFolder")) // Returns a stream of Path
.callback(Files::delete) // This would have to be called after the reader has been consumed
.map(Files::newBufferedReader); // Maps every path into a Reader
If I use peek() to delete the files, then the file won't be there when it needs to be mapped into a Reader, so I'd need something that runs after the stream is consumed.
Any idea?