Search code examples
javarx-javareactive-programmingrx-java2

File text change detection with rxjava


I want to implement some code that detect any file change (txt, doc, pdf etc.) to trigger some task with RXJava. Is it possible? Any suggestion? Thanks in advance.


Solution

  • Of course it's possible. I'd do that in the following way:

    • Use FileAlterationObserver from the Apache Commons I/O library
    • Create new observer basing on the documentation linked above (you can filter a concrete file if you don't want to monitor whole directory)
    • In the observer you can use FileAlterationListener, which has methods like onFileChange(File) required for this task
    • Create a method returning RxJava Flowable (backpressure-aware) or Observable type, which wraps listener created above - this will require some RxJava knowledge - you'll need to invoke onNext(...) method on the Emitter inside the Flowable implementation and define the type, you need to return within the Flowable - e.g. Flowable<FileEvent> where FileEvent have to be defined