• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

File move using Runtime or java.io

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gurus,

I have a java application that will execute every 15 minutes. All it has to do is move several files (10-100) from one directory to another. I found two ways to do this:

1 Calling dos command move from my program or
2 Using standard java.io classes like BufferedInputStream.....read, write

Please tell me which will be more better in terms of performance, error handling etc. I was thinking the dos way is better in performance since you dont have to read the file in the stream and then write it back again. Are there any catches?

This is how I use the dos command:
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The far easier and faster way to do this is to use the nio channels. There is an example on the [URL=http://www.javaalmanac.com/egs/java.nio/File2File.html[/URL] site.

Then after using this method to copy, simply use the File object to delete the orignal file.

Cheers,
Rachel
[ February 25, 2005: Message edited by: Rachel Swailes ]
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The very, very easiest way to move a file is to use java.io.File.renameTo(). Before copying a file using java.(n)io, you should always try renameTo(). If it fails, then fall back to copying the file contents.

renameTo() generally only works if the new location is on the same filesystem. it returns a boolean to let you know if it succeeded or not.
[ February 25, 2005: Message edited by: Ernest Friedman-Hill ]
 
Martin Lira
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for yours replies. Just curious to know why the dos way is not preferred?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Martin Lira:
Just curious to know why the dos way is not preferred?



Starting an external program can be slow, of course. But worse than that, it's non-portable -- the command to remove a file will differ not only between different OSs, but possibly even between different versions of the same OS.
 
Martin Lira
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is what i use for the move,


One problem here, if the source file is very large and still downloading while the moveFile is executed, it returns a false.
Is there any method in the File that will specify the file is still not there completely and wont call the moveFile.
I tried File.isFile, exists, canRead but they all return true even if the file download is still not complete.

thanks,
ML
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Martin Lira:
This is what i use for the move,


One problem here, if the source file is very large and still downloading while the moveFile is executed, it returns a false.



on which operating system?

different OS's have different file system semantics; on some, you should be able to move a file that's still being appended to, on others you might not. it might even depend on whatever is doing the downloading and is adding more data to the file; some programs might get really confused if you move their target file from underneath them even if the operating system itself would normally let you, other programs might take it in stride.

Is there any method in the File that will specify the file is still not there completely and wont call the moveFile.
I tried File.isFile, exists, canRead but they all return true even if the file download is still not complete.



probably because these calls have no way of knowing about any other programs that might hold the file open for writing. they check to see if the file is there, which it is, or if you can read from it, which you can. i know of no (easy) way of telling whether some other program entirely might be working on the file, short of file locking, but even that would require the other program to cooperate by acquiring a lock on the file first. it might not.
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Follow the download of a large file with a download of a dummy 'trigger' file.

When you see the trigger file, you know the main file download is complete.

Guy
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic