Can the Java File method “canWrite()” support locking?

后端 未结 5 2553
遥遥无期
遥遥无期 2021-02-19 19:16

I have a Java application that monitors a folder for incoming XML files. When a new file is detected I need to test the file that it is not currently being updated and is close

5条回答
  •  无人及你
    2021-02-19 19:48

    Additionally, if you do a check followed by a write, then you have a race condition. The state could change between the check and the write. Sometimes its best to try and do the thing you want and handle errors gracefully. perhaps an n-attempt retry mechanism with a increased fallback delay time.

    Or redefine your test. In this case, you could perhaps test that the filesize hasn't changed over a period of time before processing it.

    Another option is to split the code into two, you could have another thread -- perhaps a quartz task -- responsible for moving finished files into a different directory that your main code processes.

提交回复
热议问题