package com.itbuluoge.nio;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class GetChannel {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
/*文件内容拷贝*/
FileChannel fca=new FileInputStream("D_Log5.16.rar").getChannel();
FileChannel fcb=new FileOutputStream("test.rar").getChannel();
/*ByteBuffer swap=ByteBuffer.allocate(1000);
int c=fca.read(swap);
while(c!=-1)
{
swap.flip();
写入到新文件
fcb.write(swap);
继续读入
swap.clear();
c=fca.read(swap);
}*/
/*理想化通道拷贝*/
//fca.transferTo(0, fca.size(), fcb);
/*或者使用这个*/
fcb.transferFrom(fca, 0, fca.size());
}
}
直接使用通道直接进行数据传输。