This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have a large image file that writes to disk. When does File.exists() return true, when the file first starts writing to disk or when it is done writing to disk. If it is when it starts writing to disk, what is an easy way to tell when the file is done writing to disk? I thought of comparing the file size over time until the difference between two comparisons was 0. Any other sugestions? Thanks, Warren Bell Netricks
File.exists() returns true as soon as the underlying operating system says that the file exists; often this will be as soon as the file is opened for writing. Now, as to the second question: think of a web server log file, for a web site visited by only one person per week. Once a week, that file grows by one line. When is that file "through being written?" Now, in your case, you probably know more than you think, so you can use your special knowledge. For some image formats, the file size is in the header; you can open the file, read this number, then wait for the file to grow that big. Otherwise, if you control the software that writes the file, or know something about the source of the file, you may be able to learn this final size some other way -- i.e., a message from the software saving it. If all else fails, you might be able to read the whole file and see if it's a correct image file; if data is missing, you could keep waiting.
One other possibility you might try if you have JDK 1.4: open a RandomAccessFile on the file, use getChannel() to get the FileChannel - then use tryLock() to see if you can get an exclusive lock on the whole file. If the method returns null, that indicates another process has locked the file for writing. If you get a FileLock, cool. Do what you need to, then release the lock. This method isn't guaranteed to work on all platforms, and whether it works or not depends also on the nature of the other process that's writing the file. Is it currently writing? Does it respect the OS file locking mechanism, or does it bypass this somehow? No guarantees here - but give it a try; it my well be just what you need.
A trick used by FTP routines is to write the real file, close it, write a flag file. When the flag file shows up as existing, the first one is well and truly done. You don't really care when the second file is done.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Stan's idea actually works quite nicely. You can create a file with the exact same name and just add a suffix. So after the file file1.txt is finished being written to and is closed, you also send along file1.txt.ready. Your program is looking for files that end in .ready. When it finds one, it knows the name of the file it needs to process. It processes the file and deletes the .ready file and then goes to sleep. When it wakes up it checks for any new .ready files.