Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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 avoid is reading a file that the other process is currently in the middle of writing out, so it would be incomplete. Currently, the process that reads uses a minimum file age timer check, so it ignores all files unless their last modified date is more than XX seconds old.

I'm wondering if there is a cleaner way to solve this problem. If the filetype is unknown (could be a number of different formats) is there some reliable way to check the file header for the number of bytes that should be in the file, vs the number of bytes currently in the file to confirm they match?

Thanks for any thoughts or ideas!

like image 535
RandomUser Avatar asked Apr 05 '12 13:04

RandomUser


People also ask

How can I tell if a file is completely written?

File. canWrite() is used to check whether the file can be written to in Java. This method returns true if the file specified by the abstract path name can be written to by an application and false otherwise.

How do you check if a file is used by another process in Java?

You can run from Java program the lsof Unix utility that tells you which process is using a file, then analyse its output. To run a program from Java code, use, for example, Runtime , Process , ProcessBuilder classes.


2 Answers

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.

like image 163
John Farrelly Avatar answered Sep 24 '22 13:09

John Farrelly


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.

like image 36
Michał Kosmulski Avatar answered Sep 24 '22 13:09

Michał Kosmulski