Java NIO: Copier un fichier

Author:

string,static, java
Download

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
 
public class NIOCopier
{
 
  public static void main(String[] args) throws IOException
  {
     String in_File="c:/test.txt";
     String out_File="c:/test2.txt";
 
     copier(in_File,out_File);
 
  }
  private static void copier(String fichier_source, String fichier_dest)throws IOException
    {
     FileInputStream src = new FileInputStream(fichier_source);
    FileOutputStream dest = new FileOutputStream(fichier_dest);
 
    FileChannel inChannel = src.getChannel();
    FileChannel outChannel = dest.getChannel();
 
    for (ByteBuffer buffer = ByteBuffer.allocate(1024*1024);
         inChannel.read(buffer) != -1;
         buffer.clear()) {
       buffer.flip();
       while (buffer.hasRemaining()) outChannel.write(buffer);
    }
 
    inChannel.close();
    outChannel.close();
    }
}