Java NIO 学习笔记 读写结合补充
2024-09-05 03:50:12
小练习:nio读写文件,将fileread中的内容读取到filewrite中
try {
//创建输入通道
FileInputStream fis = new FileInputStream("F:/iotest/readfile.txt");
FileChannel readChannel = fis.getChannel();
//创建缓冲区
ByteBuffer buffer = ByteBuffer.allocate();
//创建输出通道
FileOutputStream fos = new FileOutputStream("F:/iotest/filewrite.txt");
FileChannel writeChannel = fos.getChannel(); while (true) {
buffer.clear();
//将文件内容读到缓冲区中
int mark = readChannel.read(buffer);
if (mark==-) {
break;
}
buffer.flip();
writeChannel.write(buffer);
} } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
代码运行前:
readfile:
filewrite:
代码运行后:
readfile:
filewrite:
最新文章
- String split
- cms修改栏目单页样式错位调整
- FtpWebRequest FTP异步下载、异步上传文件
- 阿里云安装nginx 和 php-fpm
- makefile实例(2)-多个文件实例
- flask+gevent+gunicorn+nginx 初试
- Spring Security(14)——权限鉴定基础
- LAMP 实现全过程及wordpress的搭建
- 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)
- CodeForces 528D Fuzzy Search 多项式 FFT
- LeetCode算法题-Best Time to Buy and Sell Stock II
- https证书随记
- 《从零玩转python+人工智能-3》120,122节课深度优先疑问解答
- &; 引用传值
- Abp问题解决集合1
- (转载)通向架构师的道路(第四天)之Tomcat性能调优-让小猫飞奔
- 【转】簡單講講 USB Human Interface Device
- Android——Activity去除标题栏和状态栏
- 大数据学习资料之SQL与NOSQL数据库
- InnoDB: The innodb_system data file 'ibdata1' must be writable错误