一、什么是NIO?
java 传统的IO是阻塞的,也叫做BIO,在读写操作完成之前,线程一直会处于阻塞状态。
而NIO则不同,他是非阻塞IO,NIO在进行读写操作时,采用管道流的方式读写,同事不断的去调用操作系统,检查操作系统是否读写完成。只有操作系统完成后才可以使用数据,这样避免了线程处于阻塞状态
二、NIO组件介绍
Channel、Buffere、Selector是NIO的核心API,IO在NIO中从一个Channel,数据可以从Channel读取到Buffer中,也可以从Buffer写入到Chaanel
(1)Channel常用的类型有一下几种:FileChannel、DatagramChannel、SocketChannel、ServerSocketChannel,这些通道分别是文件IO,UDP和TCP IO
(2)Buffer常用的有IntBuffer、FloatBuffer、CharBuffer、DoubleBuffer、ShortBuffer、LongBuffer、ByteBuffer
(3)Selectot提供了单线程处理多个Channel的能力,首先需要将Channel注册到Selector中
三、NIO的使用
1、使用FileChannel
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
public class FileChannelDemo {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
fis = new FileInputStream("C:\\Users\\42515\\Desktop\\test\\file1.txt");
File outFile = new File("C:\\Users\\42515\\Desktop\\test\\file2.txt");
if (!outFile.exists()) {
outFile.createNewFile();
}
fos = new FileOutputStream(outFile, true);
inChannel = fis.getChannel();
outChannel = fos.getChannel();
//高效拷贝文件,避免了两次用户态和内核态的上下文切换
inChannel.transferTo(0, inChannel.size(), outChannel);
ByteBuffer br = ByteBuffer.allocate(48);
while (inChannel.read(br) != -1) {
//设置限制位置为当前位置position,也就是读写指针的位置,限制位置limit指的是在该模式下最多能读写多少数据,然后将position设置为0,表示将存缓存头部开始读;
//调用flip之后,读写指针指到缓存头部,并且设置了最多只能读出之前写入的数据长度(而不是整个缓存的容量大小)
br.flip();
//写入数据
outChannel.write(br);
//将position设置为0,limit为缓冲区的大小,也就是初始化指针的位置,实际上并没有清除缓冲区的数据
br.clear();
//此处可以继续读取缓冲区的数据
System.out.println(new String(br.array(), StandardCharsets.UTF_8));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inChannel != null) {
inChannel.close();
}
if (outChannel != null) {
outChannel.close();
}
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
···