• Post Reply Bookmark Topic Watch Topic
  • New Topic

a program that moves files

 
Dan Maples
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dan Maples
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the I/O and Streams forum...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!