ChannelCopy.java

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class ChannelCopy {
  public static void main(String[] args) throws IOException {
    if (args.length!=2) {
      System.err.println("Usage: java ChannelCopy <from> <to>");
      System.exit(1);
    }
    FileInputStream in = new FileInputStream(args[0]);
    ReadableByteChannel cin = Channels.newChannel(in);
    FileOutputStream out = new FileOutputStream(args[1]);
    WritableByteChannel cout = Channels.newChannel(out);
    ByteBuffer byteB = ByteBuffer.allocate(1000);
    int nb;
    try {
      while ((nb=cin.read(byteB))!=-1) {
    byteB.flip();  // position=0, limite=fin données lues          
    cout.write(byteB);      
    byteB.clear(); // position=0, limite=capacité
      }
    } finally {
      cin.close();     // Ferme le canal et le flot !
      cout.close();
    }
  }
}