Java FileChannel file copy

Description

Java FileChannel file copy

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

public class Main {
  public static void main(String[] args) {
    File sourceFile = new File("Main.java");
    File sinkFile = new File("Main1.txt");

    try (FileChannel srcChannel = new FileInputStream(sourceFile).getChannel();
        FileChannel sinkChannel = new FileOutputStream(sinkFile).getChannel()) {

      // Copy source file contents
      srcChannel.transferTo(0, srcChannel.size(), sinkChannel);
    }catch(Exception e) {
      e.printStackTrace();/*from  w  w w . j  av  a 2s  . co m*/
    }
  }
}



PreviousNext

Related