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

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.

Leave a Comment