views:

6326

answers:

6

Is there a standard Java library that handles common file operations such as moving/copying files/folders?

+11  A: 

Not yet, but the New NIO (JSR 203) will have support for these common operations.

In the meantime, there are a few things to keep in mind.

File.renameTo generally works only on the same file system volume. I think of this as the equivalent to a "mv" command. Use it if you can, but for general copy and move support, you'll need to have a fallback.

When a rename doesn't work you will need to actually copy the file (deleting the original with File.delete if it's a "move" operation). To do this with the greatest efficiency, use the FileChannel.transferTo or FileChannel.transferFrom methods. The implementation is platform specific, but in general, when copying from one file to another, implementations avoid transporting data back and forth between kernel and user space, yielding a big boost in efficiency.

erickson
nice too :) good stuff
Peter Perháč
+1  A: 

java.io.File can rename (move) files and folders. The classes in java.io could be used to implement copy operations as well.

mipadi
+3  A: 

Check out: http://commons.apache.org/io/

It has copy, and as stated the JDK already has move.

Don't implement your own copy method. There are so many floating out there...

Pyrolistical
Commons IO has limitations with respect to the size of files it can copy. For a general-purpose solution, a more robust implementation would be expected.
erickson
Implementing one's own copy method is trivial and means you won't be dependent on an entire library. *Do* implement your own
oxbow_lakes
Copy method is far from trivial. You can easily make a correct one that doesn't perform using Streams, and fast but incorrect one using NIO. Never implement your own utilities when there are quality libraries out there.
Pyrolistical
A copy method *is* non-trivial, and Apache Commons can't handle a common use case: information too large for main memory. A library intended for managing mass storage should have bounds on its memory-consumption, which Apache Commons move method lacks.
erickson
+1  A: 

Use FileUtils of http://commons.apache.org/io It's realy simple.

damian
The IOUtils from the same library is also very useful.
Peter Lawrey
+8  A: 

I'll just try to update this with java.nio operations, in case someone searches for this again:

public static void copyFile(File sourceFile, File destFile) throws IOException {
 if(!destFile.exists()) {
  destFile.createNewFile();
 }

 FileChannel source = null;
 FileChannel destination = null;
 try {
  source = new FileInputStream(sourceFile).getChannel();
  destination = new FileOutputStream(destFile).getChannel();
  destination.transferFrom(source, 0, source.size());
 }
 finally {
  if(source != null) {
   source.close();
  }
  if(destination != null) {
   destination.close();
  }
}
Rigo Vides
if the file exists will the content be append or overwritten?
Janusz
+2  A: 

Google's Guava library also has these:

http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/io/Files.html

Andrew McKinlay