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

前端 未结 5 666
庸人自扰
庸人自扰 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:05

    A generic solution to this problem seems impossible from the "consumer" end. The "producer" may temporarily close the file and then resume appending to it. Or the "producer" may crash, leaving an incomplete file in the file system.

    A reasonable pattern is to have the "producer" write to a temp file that's not monitored by the "consumer". When it's done writing, rename the file to something that's actually monitored by the "consumer", at which point the "consumer" will pick up the complete file.

提交回复
热议问题