Java NIO 文件通道使用
2024-10-20 09:07:49
读取一个文件的内容,然后写入另外一个文件
public class NioTest4 { public static void main(String[] args) throws Exception {
FileInputStream inputStream = new FileInputStream("input.txt"); FileOutputStream outputStream = new FileOutputStream("output.txt"); FileChannel inputChannel = inputStream.getChannel();
FileChannel outputChannel = outputStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024);
while (true){
buffer.clear();
int read = inputChannel.read(buffer);
if( -1 == read){
break;
}
buffer.flip(); outputChannel.write(buffer); }
inputChannel.close();
outputChannel.close(); } }
通过NIO读取文件涉及3个步骤
1、从FileInputStream获取FileChannel对象
2、创建Buffer
3、将数据从Channel读取到Buffer中
绝对方法与相对方法的含义
1、相对方法: limit值与position值会在操作时被考虑到
2、绝对方法: 完全忽略调limit值和position值。
最新文章
- three.js笔记
- Redis安全访问
- POJ3041Asteroids(最小点覆盖+有点小抽象)
- BZOJ3823 : 定情信物
- UltraEdit20 注册
- java使用ms-dos编译,运行程序
- Android开发:最详细的 NavigationDrawer 开发实践总结
- 学习笔记 之--AJAX核心对象 XMLHttpRequest
- JavaWeb验证码的使用
- Redis服务器搭建
- NNSZ OIers' Blog Archive
- js动态加载js css文件,可以配置文件后辍,防止浏览器缓存
- php5.5.7添加pgsql,pdo_pgsql,swoole
- python数据类型--set(集合)
- LR 两种html与url录制
- [C++]指针与引用(定义辨析)
- pyhon 之 数据类型详解
- webpack 学习
- day 19 类的名称空间,组合,派生
- android studio svn 创建分支