This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes File race condition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "File race condition" Watch "File race condition" New topic
Author

File race condition

John Landon
Ranch Hand

Joined: Sep 25, 2008
Posts: 227
Hi,
I have a process COPY that copies files from dir A to dir B.
I have to write a program that grabs files from dir B, processes them and moves them to dir C.
My question is what happens if process COPY have not finished writing the file but my new process already grabed it. So it will be incomplete??..
Any ideas?

Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yes, that will be trouble if you do that. So arrange your file creation protocol so that doesn't happen. Don't allow the writing task to write in a place where the reading task is looking.

So for example you could have the writing task write files into directory X, then move them into A when they are complete. (If X and A are on the same drive then that just changes pointers in the file system, so you can treat it as atomic.)

Or you could have the reading task only read files with a certain property (such as the extension not being ".notdoneyet"). Then the writing task writes files with that extension, and renames them when it's finished.

Don't bother with clever solutions like waiting for the file size to stop changing, or putting locks on the files. I've tried them and they never worked reliably for me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37900
    
  22
I would suggest you try it, and tell us what happens. I suspect that until the file has been completely written, you will suffer FileNotFoundExceptions. But I am not sure.
John Landon
Ranch Hand

Joined: Sep 25, 2008
Posts: 227
is there a way to check if the file is in use?
John Landon
Ranch Hand

Joined: Sep 25, 2008
Posts: 227
Paul Clapham wrote:Yes, that will be trouble if you do that. So arrange your file creation protocol so that doesn't happen. Don't allow the writing task to write in a place where the reading task is looking.

So for example you could have the writing task write files into directory X, then move them into A when they are complete. (If X and A are on the same drive then that just changes pointers in the file system, so you can treat it as atomic.)

Or you could have the reading task only read files with a certain property (such as the extension not being ".notdoneyet"). Then the writing task writes files with that extension, and renames them when it's finished.

Don't bother with clever solutions like waiting for the file size to stop changing, or putting locks on the files. I've tried them and they never worked reliably for me.


how do I check that? "when they are complete"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File race condition
 
Similar Threads
NX Contractor - startup mode
rsync blank spaces problem
I want my ant script Implement a function
Directory/File comparison tool using Java.
Show dir and file structure exactly as it is