Hi friends, I’m stuck in this problem with flows: ...
# announcements
e
Hi friends, I’m stuck in this problem with flows: I need to have a flow from a file, but keeping it open (as a “tail -f” on the file). I attach more info just in this same thread to keep it “readable”… THANKS A LOT
Hi friends, I’m stuck in this problem with flows: I need to have a flow from a file, but keeping it open (as a “tail -f” on the file). I started with something as:
Copy code
val stream: Flow<String> = File(arguments.filename).readLines().asFlow()
I am consuming the flow with something as:
Copy code
runBlocking {
    stream
    .map { <whatever> }
    .filter { <whatever> }
    .collect { println(it) }
...
}
🔴 I know that
readLines
gets all content and closes the stream. but and I though it could be easy to “iterate” to the next version that do not close the stream over the file. BUT I CANNOT FIND HOW TO KEEP THE FLOW “LIVE” ?? Some idea? PD: I tried to use a Timer to run periodically the “collector” but in this case the file is always read “from the beggining”… 😞
s
Just Google "Java file watcher" you can register a watch service which receives updates when a directory changes and then filter to a specific file. Wrapping it as a Flow should be straight forward
e
Thanks I’ll take a look