How to test if a file is “complete” (completely written) with Java

后端 未结 10 1630
再見小時候
再見小時候 2020-12-05 04:21

Let\'s say you had an external process writing files to some directory, and you had a separate process periodically trying to read files from this directory. The problem to

10条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 05:16

    The way I've done this in the past is that the process writing the file writes to a "temp" file, and then moves the file to the read location when it has finished writing the file.

    So the writing process would write to info.txt.tmp. When it's finished, it renames the file to info.txt. The reading process then just had to check for the existence of info.txt - and it knows that if it exists, it has been written completely.

    Alternatively you could have the write process write info.txt to a different directory, and then move it to the read directory if you don't like using weird file extensions.

提交回复
热议问题