aspose file tools
The moose likes Java in General and the fly likes Locking files using java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Locking files using java" Watch "Locking files using java" New topic
Author

Locking files using java

Sumita Prasad
Greenhorn

Joined: Feb 07, 2003
Posts: 2
Hi,
This is the problem statement,
In our project we are using ftp to transfer zip files from different machines.
Once the file get's transferred the local machine it is picked up by the application running on the local machine.Both this application is running in asynchronous maode.
The problem is, some time application running in the local machine,tries to pick up the file before it get's fully transferred, there by giving some error, such as invalid content.
Is there any way to prevent this, i.e. to make the file visible only after, the file is completely transferred.
Thanks
Sumita
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
This ought to get around the problem of using locks:
Download the data into a temporary file in another directory. When the download is completely finished, use File.rename() to put it where it belongs.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
Alternately JDK 1.4 provides classes that allow you to lock a file:

Note that the effectiveness of this depends on how (or if) locking is implemented by the operating system. It usually works pretty well - but if you want to be more certain that no one will be messing with the file until it's downloaded, do as David suggests. (This also works if you're using an older JDK.)


"I'm not back." - Bill Harding, Twister
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
A low-tech way of handling this is to modify the application to pick up the file only if it has not been modified for a certain period of time.
- Peter
 
 
subject: Locking files using java
 
Threads others viewed
Read .dsp file cont.
Latest Class files not picked up by Tomcat
File Lock using Java
Error 500: LinkageError while defining class:
Upload a file to unix and invoke a java method
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com