Java FileChannel get from FileInputStream

Description

Java FileChannel get from FileInputStream

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  ww  . j  av a  2 s  .c o m
    }
  }
}



PreviousNext

Related