GeeCON Prague 2014*
The moose likes Java in General and the fly likes How to copy files in Java code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "How to copy files in Java code?" Watch "How to copy files in Java code?" New topic
Author

How to copy files in Java code?

Nancy Zhang
Greenhorn

Joined: Jan 07, 2005
Posts: 25
I'm wondering whether there's some Java class/method to help copy files.

For example, my code wants to do this:
if specified file is a .java file, then ... certain processing...
else just copy this non-java file to a destination directory.

Thanks.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Check this out. Should work for files or directories.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hmmm, reading all the responses to that article I'd be a bit shy about using it. Usually the answer is you're on your own to read the old file and write the new one. If you have to copy directories you need to detect the source directory and create the target directory. To copy bytes without corruption, use some streams for the job. Take a look at the JavaDoc, see if you can find classes that look appropriate. Give it a shot and let us see what you come up with!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
http://jakarta.apache.org/commons/io/apidocs/org/apache/commons/io/IOUtils.html


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
For that article I posted, if you are sure you will be moving files around on the same file system (not crossing partitions, ect...) the renameTo() function is fine. Otherwise I've heard the apache IOUtils mentioned above are very nice (although I haven't used them).
Nancy Zhang
Greenhorn

Joined: Jan 07, 2005
Posts: 25
Great thanks for all responses. Will try the apache IOUtils.
 
GeeCON Prague 2014
 
subject: How to copy files in Java code?