什么是NIO
JDK1.4版本开始,JDK提供了新的IO操作API, NIO提供多路(non-blocking) 非阻塞式的高伸缩性网络I/O,从而提高了效率,NIO主要有三大核心组件:Channel、Buffer和Selector,这里重点学习前两个。
Buffer
•Buffer是一个抽象类,Buffer类型变量对应的对象代表一块缓冲区,ByteBuffer、CharBuffer、DoubleBuffer、FloatBuffer、IntBuffer、LongBuffer和ShortBuffer类都是Buffer抽象类的子类,其中ByteBuffer最常用。
•ByteBuffer常用方法:(以下方法都在main方法之中实验)
1.static ByteBuffer allocate(int capacity):分配一个新的字节缓冲区。√
2.int capacity() :返回此缓冲区的容量。
ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
int size=byteBuffer.capacity();
3.ByteBuffer put(byte b):将字节类型数据写入当前位置的缓冲区,然后当前位置+1,位置从0开始。
ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
int size=byteBuffer.capacity();
System.out.println(size);
byte a= 9;
byteBuffer.put(a);
System.out.println(byteBuffer.position());
输出结果是
1024
1
4.byte[] array() :将ByteBuffer类型的数据转为byte数组。√
5.int position():返回缓冲区当前位置。
6.Buffer flip() ):翻转缓冲区,将position置零。√(但不清除缓存区里面的数据,只是将位置置零)
ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
int size=byteBuffer.capacity();
System.out.println(size);
byte a= 9;
byteBuffer.put(a);
System.out.println(byteBuffer.position());
byteBuffer.flip();
System.out.println(byteBuffer.get());
System.out.println(byteBuffer.position());
见这个博客,把filp和remain诉说的非常详细https://blog.youkuaiyun.com/u013096088/article/details/78638245
7.boolean hasRemaining():判断缓冲区是否含有数据 。
填充元素 buffer.hasRemaining()用于判断缓冲区是否达到上界limit。
该填充过程等效于:int remainCount = buffer.remaining();
for (int j = 0; j < remainCount;
* j++){buffer.put((byte) j++);}
8.byte get()读取缓冲区当前位置的字节,然后当前位置+1。√
9.Buffer clear():清除缓冲区,位置归零。√
Channel
•Channel是一个接口,该接口类型变量指向的对象代表一个数据传输通道,Channel对象是面向缓冲区的:数据总是从通道读取到缓冲区(Buffer类型对象),或从缓冲区(Buffer类型对象)写入到通道中。
•Channel接口主要实现类如下:
1.FileChannel:从文件中读写数据。
2.DatagramChannel:通过UDP读写网络中的数据。
3.SocketChannel:通过TCP读写网络中的数据。
4.ServerSocketChannel:可以监听新进来的TCP连接,像Web服务器那样,对每一个新进来的连接都会创建一个SocketChannel。
•只能通过调用FileInputStream和FileOutputStream类中getChannel方法获取FileChannel对象,FileChannel类常用方法如下:
1.int read(ByteBuffer dst):从通道的当前文件位置开始将数据读取到缓冲区,然后以实际读取的字节数更新文件位置;返回实际读取的字节数,如果已达到通道末尾, 则返回-1 。
2.void close():关闭通道。
3.int write(ByteBuffer src):从通道的当前文件位置开始将缓冲区中数据写入通道,然后文件位置用实际写入的字节数更新。
try {
FileInputStream fileInputStream = new FileInputStream("G:\\手机测评\\123.txt");
FileChannel fileChannel = fileInputStream.getChannel();//建立通道
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);//设置缓冲区的容量
while (fileChannel.read(byteBuffer)!=-1) {
String a=new String(byteBuffer.array(), 0, byteBuffer.position());
System.out.println(a);
byteBuffer.clear();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
FileInputStream fileInputStream = new FileInputStream("G:\\手机测评\\123.txt");
FileChannel inputChannel = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream("G:\\手机测评\\456.txt");
FileChannel outputChannel = fileOutputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
while (inputChannel.read(byteBuffer)!=-1) {
byteBuffer.flip();
outputChannel.write(byteBuffer);
byteBuffer.clear();
}