Java IO Tutorial - Java FileChannel.transferTo(long position, long count, WritableByteChannel target)








Syntax

FileChannel.transferTo(long position, long count, WritableByteChannel target) has the following syntax.

public abstract long transferTo(long position,  long count,  WritableByteChannel target)    throws IOException

Example

In the following code shows how to use FileChannel.transferTo(long position, long count, WritableByteChannel target) method.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
//from w ww  .  j a  v  a  2 s .  c o  m
public class Main {

  public static void main(String[] args) throws Exception{
    String fromFileName = args[0];
    String toFileName = args[1];
    FileChannel in = new FileInputStream(fromFileName).getChannel();
    FileChannel out = new FileOutputStream(toFileName).getChannel();
    in.transferTo(0, (int) in.size(), out);
    in.close();
    out.close();
  }
}

The code above generates the following result.