FileInputStream fInputStream=
new FileInputStream(
new File("/root/Desktop/testImage.jpg"
));
FileOutputStream fOutputStream =
new FileOutputStream(
new File("/root/Desktop/testImage2.jpg"
));
FileChannel fcIn=
fInputStream.getChannel();
FileChannel fcOut=
fOutputStream.getChannel();
ByteBuffer buffer=ByteBuffer.allocate(1024
);
//将管道的数据读入buffer中
while(fcIn.read(buffer)!=-1
){
//由读操作转到写操作时必须调用flip来改变buffer的状态
//内部操作:limit=position;position=0
buffer.flip();
//把buffer的数据写入管道
fcOut.write(buffer);
//清空buffer
//内部操作:limit=capacity;position=0
buffer.clear();
}
fcIn.close();
fInputStream.close();
fcOut.close();
fOutputStream.close();
转载于:https://www.cnblogs.com/DajiangDev/p/3921601.html
相关资源:JAVA上百实例源码以及开源项目