public class NIOFileChannel {
public static void main(String[] args) throws Exception {
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\xiatian\\Desktop\\1.txt");
FileChannel channel = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\xiatian\\Desktop\\2.txt");
FileChannel outChannel = fileOutputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
// read从channel读到缓冲区
while(-1 != channel.read(byteBuffer)){
byteBuffer.flip();
// write从缓冲区写到channel中
outChannel.write(byteBuffer);
byteBuffer.clear();
}
fileInputStream.close();
fileOutputStream.close();
}
}