Java IO Tutorial - Java FileChannel .transferFrom (ReadableByteChannel src, long position, long count)








Syntax

FileChannel.transferFrom(ReadableByteChannel src, long position, long count) has the following syntax.

public abstract long transferFrom(ReadableByteChannel src,  long position,  long count)    throws IOException

Example

In the following code shows how to use FileChannel.transferFrom(ReadableByteChannel src, long position, long count) method.

//  w  w  w  . j av  a  2  s  . co  m
     
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

public class Main {

  public static void main(String[] args) {
    FileChannel in = null;
    FileChannel out = null;

    if (args.length < 2) {
      System.out.println("Usage: java Copy <from> <to>");
      System.exit(1);
    }

    try {
      in = new FileInputStream(args[0]).getChannel();
      out = new FileOutputStream(args[1]).getChannel();
      out.transferFrom(in, 0L, (int) in.size());

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}

The code above generates the following result.