Java NIO之用Channel和MappedByteBuffer高效快速複製大文件

NIO的應用,複製大文件,超爽,簡單高效

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

public class Demo05 {
public static void main(String[] args) throws Exception {
    FileChannel inChannel=new RandomAccessFile("f:\\01.wmv", "r").getChannel();
    FileChannel outChannel=new RandomAccessFile("f:\\02.wmv", "rw").getChannel();
    
    MappedByteBuffer map=inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());
    
    outChannel.write(map);
    outChannel.close();
    inChannel.close();
    System.out.println("複製完畢");
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章