Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

trouble using apache file.rename method  RSS feed

 
Peter Bergoff
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am attempting to "rename" (i.e. moving) files from a source directory to a destination directory and running into a problem.

Note: The files which are to be moved are being ftp'd to the server from external sources before this method is used.

Once the files are on the server the rename method is used to move them to a destination directory on the same server.

Before moving the files with the rename method I build a list of files which are in the source directory. The program then reads this list and loops through the list of files moving each file in the source directory on to the destination directory.

However, in some cases the last file in the list is not moved and an error occurs. I have no idea why in some cases the last file can't be moved. It appears that it has finished being ftp'd in to the source directory before the rename method is attempted.

Any ideas out there?

Thanks,

Peter
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Offhand, it sounds likely that the FTP is not fully complete as far as the last file is concerned. What makes you think that it is? That is, how do you know? Some things you might try here include:
  • When a rename fails, wait a short period and try again. (Experiment to see how long a wait is appropriate.)
  • Use java.nio.FileChannel to lock() on a file before processing it. This should (hopefully) block if FTP is still writing the file. (Test if this works on your system.) You will then quite possibly need to release the lock before you rename the file - but at least by that point you know the FTP is complete.
  • Require FTP clients to write their file into a temp directory first, then rename it to the target directory - so that it doesn't appear in the target dir until it's ready
  • Require FTP clients to follow some other protocol to indicate when they're done. E.g. create a new file signalling the other files are ready.


  • [ January 13, 2006: Message edited by: Jim Yingst ]
     
    Peter Bergoff
    Ranch Hand
    Posts: 103
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for replying Jim! Good thoughts for me to consider. One update: I've alreadied tried the file lock and although it states the file can be locked it still creates this problem.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!