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.
Asked
Active
Viewed 286 times
1
-
use a FileObserver – Mike_Jr Nov 06 '18 at 13:34
-
It also works with no structured data such as xlsx or similar? – Vitto Lib Nov 06 '18 at 13:41
-
@Mike_Jr `FileObserver` is a class from the Android API. In this question there's no information or tag indicating that the task is Android-specific. – Piotr Wittchen Nov 06 '18 at 14:38
1 Answers
1
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) orObservable
type, which wraps listener created above - this will require some RxJava knowledge - you'll need to invokeonNext(...)
method on theEmitter
inside theFlowable
implementation and define the type, you need to return within theFlowable
- e.g.Flowable<FileEvent>
whereFileEvent
have to be defined

Piotr Wittchen
- 3,853
- 4
- 26
- 39