Copying One File to Another with FileChannel - Java File Path IO

Java examples for File Path IO:File Copy

Description

Copying One File to Another with FileChannel

Demo Code

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class Main {
  public void m() {
    try {/* w ww .  j  av a2  s .c  om*/
      // Create channel on the source
      FileChannel srcChannel = new FileInputStream("srcFilename").getChannel();

      // Create channel on the destination
      FileChannel dstChannel = new FileOutputStream("dstFilename").getChannel();

      // Copy file contents from source to destination
      dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

      // Close the channels
      srcChannel.close();
      dstChannel.close();
    } catch (IOException e) {
    }
  }
}

Related Tutorials