NIO
ava NIO ( New IO )是从 Java 1.4 版本开始引入的一个新的 IO API ,
可以替代标准的 Java IO API 。NIO 与原来的 IO 有同样的作用和目的,但是使用的方式完全不同, NIO 支持面向缓冲区的、基于通道的 IO 操作。 NIO 将以更加高效的方式进行文件的读写操作.
通道与缓冲区
通道表示打开到 IO 设备 ( 例如:文件、套接字 ) 的连接。若需要使用 NIO 系统,需要获取用于连接 IO 设备的通道以及用于容纳数据的缓冲区。然后操作缓冲区,对数据进行处理。
Buffer 就像一个数组,可以保存多个相同类型的数据。
根据数据类型不同 (boolean 除外 ) ,有以下 Buffer 常用子类:
ByteBuffer
CharBuffer
ShortBuffer
IntBuffer
LongBuffer
FloatBuffer
DoubleBuffer
上述 Buffer 类 他们都采用相似的方法进行管理数据,只是各自
管理的数据类型不同而已。都是通过如下方法allocate(int capacity)获取一个 Buffer对象
缓冲区的属性
- 容量 (capacity) : 表示 Buffer 最大数据容量,缓冲区容量不能为负,并且创
建后不能更改。 - 限制 (limit) : 第一个不应该读取或写入的数据的索引,即位于 limit 后的数据
不可读写。缓冲区的限制不能为负,并且不能大于其容量。 - 位置 (position) : 下一个要读取或写入的数据的索引。缓冲区的位置不能为
负,并且不能大于其限制 - 标记 (mark) 与重置 (reset) : 标记是一个索引,通过 Buffer 中的 mark() 方法
指定 Buffer 中一个特定的 position ,之后可以通过调用 reset() 方法恢复到这
个 position.
创建一个缓冲区对象
static XxxBuffer allocate(int capacity) : 创建一个容量为 capacity 的 XxxBuffer 对象
int capacity = byteBuffer.capacity();
int position = byteBuffer.position();
int limit = byteBuffer.limit();
缓冲区的一些方法
缓冲区存取数据的两个核心方法:
- put() : 存入数据到缓冲区中
- get() : 获取缓冲区中的数据
- flip(); 切换读取数据模式
- rewind() : 可重复读
- clear() : 清空缓冲区. 但是缓冲区中的数据依然存在,但是处于“被遗忘”状态
- mark() : 标记是一个索引,通过 Buffer 中的 mark() 方法
指定 Buffer 中一个特定的 position ,之后可以通过调用 reset() 方法恢复到这
个 position.
读数据:
byte[] bytes = new byte[byteBuffer.limit()];
byteBuffer.get(bytes);
System.out.println(new String(bytes, 0, byteBuffer.limit()));
//可重复读取
byteBuffer.rewind();
//清空缓冲区
byteBuffer.clear();
使用缓冲区的方法,我们来存一个String
String str="abcde";
ByteBuffer byteBuffer = ByteBuffer.allocate(10);
//position 5 limit 10
byteBuffer.put(str.getBytes());
//切换读取模式 position 0 limit 5
byteBuffer.flip();
//我想读取两个字节
byte[] bytes = new byte[byteBuffer.limit()];
byteBuffer.get(bytes,0,2);
System.out.println(byteBuffer.position());
//mark标记 我可以标记一下当前position的位置
byteBuffer.mark();
//我再读取两个字节
byteBuffer.get(bytes,2,2);
System.out.println(byteBuffer.position());
//我想回到上一次标记position的位置 使用reset();就可以回到上一次标记的位置
byteBuffer.reset();
System.out.println(byteBuffer.position());
//hasRemaining()看一下还有没有可读数据
if(byteBuffer.hasRemaining()){
System.out.println(byteBuffer.remaining()); //remaining()还有多少可读取数据
}
通道 FileChannel
通道( Channel ):由 java.nio.channels 包定义的。 Channel 表示 IO 源与目标打开的连接。
Channel 类似于传统的“流”。只不过 Channel本身不能直接访问数据, Channel 只能与Buffer 进行交互。
获取通道
获取通道的一种方式是对支持通道的对象调用
getChannel() 方法。支持通道的类如下:
本地I/O
FileInputStream
FileOutputStream
RandomAccessFile
采用通道对文件的复制
//采用NIO来复制本地文件
FileInputStream in = new FileInputStream("歌曲串烧.mp3");
FileOutputStream out = new FileOutputStream("歌曲串烧2.mp3");
//获取通道
FileChannel inChannel = in.getChannel();
FileChannel outChannel = out.getChannel();
//面向通道,和缓冲区来复制文件
//分配一个非直接缓冲区
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//读写文件
while (inChannel.read(byteBuffer)!=-1){
//切换读取模式
byteBuffer.flip();
//写数据
outChannel.write(byteBuffer);
//清空缓冲区
byteBuffer.clear();
}
//释放资源
in.close();
out.close();
inChannel.close();
outChannel.close();
获取通道的另一种方式是通过FileChannel中的静态方法 open()可以打开一个通道,它由两个参数,
StandardOpenOption.CREATE_NEW 文件不存在就创建,存在就报错
StandardOpenOption.CREATE 文件不存在,就创建,存在 就覆盖
我们使用第二种方式对文件进行复制
//曹永非直接缓冲区来读取文件
FileChannel inChannel = FileChannel.open(Paths.get("F:\\IDEAproject\\Exercise\\src\\歌曲串烧.mp3"), StandardOpenOption.READ);
FileChannel outChannel = FileChannel.open(Paths.get("F:\\IDEAproject\\Exercise\\src\\歌曲串烧3.mp3"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 8);
while (inChannel.read(byteBuffer)!=-1){
byteBuffer.flip();
outChannel.write(byteBuffer);
byteBuffer.clear();
}
inChannel.close();
outChannel.close();
//使用直接缓冲区来复制文件
FileChannel inChannel = FileChannel.open(Paths.get("C:\\Users\\ShenMouMou\\Desktop\\demo.zip"), StandardOpenOption.READ);
FileChannel outChannl = FileChannel.open(Paths.get("E:\\demo5.zip"), StandardOpenOption.READ,StandardOpenOption.WRITE, StandardOpenOption.CREATE);
MappedByteBuffer inMap = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
MappedByteBuffer outMap = outChannl.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());
//把数据读取到缓冲区中
byte[] bytes = new byte[inMap.limit()];
inMap.get(bytes);
outMap.put(bytes);
inChannel.close();
outChannl.close();