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

You could use an external marker file. The writing process could create a file XYZ.lock before it starts creating file XYZ, and delete XYZ.lock after XYZ is completed. The reader would then easily know that it can consider a file complete only if the corresponding .lock file is not present.


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.


I had no option of using temp markers etc as the files are being uploaded by clients over keypair SFTP. they can be very large in size.

Its quite hacky but I compare file size before and after sleeping a few seconds.

Its obviously not ideal to lock the thread but in our case it is merely running as a background system processes so seems to work fine

private boolean isCompletelyWritten(File file) throws InterruptedException{
    Long fileSizeBefore = file.length();
    Thread.sleep(3000);
    Long fileSizeAfter = file.length();

    System.out.println("comparing file size " + fileSizeBefore + " with " + fileSizeAfter);

    if (fileSizeBefore.equals(fileSizeAfter)) {
        return true;
    }
    return false;
}

Note: as mentioned below this might not work on windows. This was used in a Linux environment.


One simple solution I've used in the past for this scenario with Windows is to use boolean File.renameTo(File) and attempt to move the original file to a separate staging folder:

boolean success = potentiallyIncompleteFile.renameTo(stagingAreaFile);

If success is false, then the potentiallyIncompleteFile is still being written to.

Tags:

Java