• Post Reply Bookmark Topic Watch Topic
  • New Topic

Locking files using java  RSS feed

 
Sumita Prasad
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!