• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a program that moves files

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the I/O and Streams forum...
[ September 05, 2004: Message edited by: Dirk Schreckmann ]
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic