Forums Register Login

a program that moves files

+Pie Number of slices to send: Send
Hey guys, I was wondering if it is possible to write a program that moves files from one place to another. Basicly what I want to do is move a file from one folder to another using a java program, and if possible rename that file at the same time. If this is possible could somebody let me know and also point me in a good direction to start.
Thanks
Dan
+Pie Number of slices to send: Send
Well, the java.io.File class has a renameTo() method, which can move and rename files around on the same filesystem (same volume, in Windows-speak.) So a simple version could just use this method.

But renameTo() can fail, and it indicates failure by returning "false". To move a file that renameTo() can't move, you need to open the old file with FileInputStream, open the new location with FileOutputStream, read the bytes from the input, write all the bytes to the output, close the streams and delete the input file. The I/O section of the Java Tutorial would be helpful to you in implementing this.
+Pie Number of slices to send: Send
well I was hoping to write a program to organize my music into folders with artist's names and album, but it sound like this won't work very well for that. Thanks for the reply though
+Pie Number of slices to send: Send
I don't see how you come to that conclusion, but if your goal is just to have a program that does this (rather than learning how to write one), check out Apple iTunes (available for Mac and PC).
+Pie Number of slices to send: Send
 

Originally posted by Ernest Friedman-Hill:
To move a file that renameTo() can't move, you need to open the old file with FileInputStream, open the new location with FileOutputStream, read the bytes from the input, write all the bytes to the output, close the streams and delete the input file.



Or you just use the FileUtils of Jakarta Commons IO to copy the file.
+Pie Number of slices to send: Send
Moving this to the I/O and Streams forum...
[ September 05, 2004: Message edited by: Dirk Schreckmann ]
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 613 times.
Similar Threads
Class File - Any move and/or copy method()?
accessing java files within folders
(104) Connection reset by peer
copying files over the internet with sockets
Learning about programming web applications
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:59:25.