Menu Home

使用FileChannel拷贝文件

使用FileChannel拷贝文件的效率比逐字节拷贝要更高效,但是FileChannel有一个限制:操作的文件大小不能大于2GB

public void copy(File src, File dst) throws IOException {
    FileInputStream inStream = new FileInputStream(src);
    FileOutputStream outStream = new FileOutputStream(dst);
    FileChannel inChannel = inStream.getChannel();
    FileChannel outChannel = outStream.getChannel();
    inChannel.transferTo(0, inChannel.size(), outChannel);
    inStream.close();
    outStream.close();
}

Categories: java

Tagged as:

lnmcc

Leave a Reply

Your email address will not be published.