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

后端 未结 5 2552
遥遥无期
遥遥无期 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:49

    If you control both the reader and writer, then a potential locking technique would be to create a lock directory -- which is typically an atomic operation -- for the read and the write process duration. If you take this type of approach, you have to manage the potential failure of a process resulting in a "hanging" lock directory.

    As Cheekysoft mentioned, files are not atomic and are ill suited for locking.

    If you don't control the writer -- for instance if it's being produced by an FTP daemon -- then the rename technique or delay for time span technique are your best options.

提交回复
热议问题