File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes When does File.exists() return true? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "When does File.exists() return true?" Watch "When does File.exists() return true?" New topic

When does File.exists() return true?

Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
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?
Warren Bell
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24187

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.

[Jess in Action][AskingGoodQuestions]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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.

"I'm not back." - Bill Harding, Twister
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
I agree. Here's the link:
subject: When does File.exists() return true?