NIO学习:buffer读入与写出(文件复制示例)
2024-10-16 10:32:04
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();
最新文章
- JVM内存管理------垃圾搜集器简介
- Samsung S4卡屏卡在开机画面的不拆机恢复照片一例
- Android快速开发框架LoonAndroid (转)
- BZOJ1004: [HNOI2008]Cards
- 跨站脚本 XSS<;一:防御方法>;
- poj1417 带权并查集+0/1背包
- Delphi 使用串口模拟工具进行串口程序开发调试
- MySQL相关知识
- JavaScript高级程序设计48.pdf
- Factorial Solved Problem code: FCTRL
- JavaScript中的execCommand()命令详解及实例展示
- JS放大镜特效(兼容版)
- 《经久不衰的Spring框架:SpringMVC 统括》
- HDFS Java API的使用举例
- JSONPath使用
- 数组中array==null和array.length==0的区别
- html问题汇总
- 四则运算Java语言实验设计过程1
- 【BZOJ】【2127】happiness
- c# 通过文件夹共享复制文件到服务器