Avoid detecting incomplete files when watching a directory for changes in java

前端 未结 5 665
庸人自扰
庸人自扰 2020-12-31 11:34

I am watching a directory for incoming files (using FileAlterationObserver from apache commons).

class Example implements FileAlterationListener {
    public         


        
5条回答
  •  被撕碎了的回忆
    2020-12-31 12:08

    I don't think you can achieve what you want unless you have some file system constraints and guarantees. For example, what if you have the following scenario :

    • File X created
    • A bunch of change events are triggered that correspond with writing out of file X
    • A lot of time passes with no updates to file X
    • File X is updated.

    If file X cannot be updated after it's written out, you can have a thread of execution that calculates the elapsed time from the last update to now, and after some interval decides that the file write is complete. But even this has issues. If the file system is hung, and the write does not occur for some time, you could erroneously conclude that the file is finished writing out.

提交回复
热议问题