Java Snippet – Fast File Copy

When using JDK 1.4 and above...

JAVA:
  1. public static void copyFile(File in, File out) {
  2.     try {
  3.         FileChannel sourceChannel = new FileInputStream(in).getChannel();
  4.         FileChannel destinationChannel = new FileOutputStream(out).getChannel();
  5.         sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
  6.         // or, you can also copy it this way
  7.         // destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
  8.         sourceChannel.close();
  9.         destinationChannel.close();
  10.     } catch ( Exception e ) {
  11.         e.printStackTrace();
  12.     }
  13. }

No Comments so far
Leave a comment



Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

(required)

(required, but not displayed publically)