#NIO
* 面向缓冲区(双向的)
* 非阻塞的
#通道和缓冲区
* 通道(Channel):用于传输
* 缓冲区(Buffer):用于存储
#缓冲区的种类
* ByteBuffer
* IntBuffer
* DoubleBuffer
* FloatBuffer
* CharBuffer
#缓冲区的获取
* allocte()获取缓冲区
* 缓冲区的创建 ByteBuffer bt = ByteBuffer.allocate(1024);
* position:当前的位置
* limit:limit之前的数据可用于读
* capactiy:缓冲区的容量大小
* flip()切换到读模式
*position:在读的过程中position的位置在不断的移动
* bt.flip();//切换到读模式
* get()方法用于读
* put()方法用于写
* rewind():可重复读,使得position的位置到开始的地方,可以重 新的读
*
* bt.clear();//清空缓冲区,数据仍在缓冲区只是被遗忘了
* bf.mark();//标记一下 position的位置
* bf.reset();//position重新回到mark的地方
#通道Channel(用于传输数据)
* channel本身不能进行数据的访问,只能和Buffer进行交互
* 可以生成通道的类
* FileChannel:用于读取,写入,映射和操作文件的通道
* DatagramChannel:通过udp读写网络中的数据通道
* SocketChannel:通过tcp读写网络中的数据
* ServerSocketChannel:可以监听新进来的Tcp连接
* 通道获取方式
* 调用 getChannel()方法,支持的类有
* FileInputStream
* FileOutPutStream
* RandomAccessFile
* DatagramSocket
* Socket
* ServerSoccket
* File的静态方法 new ByteChannel()获取字节通道
FileInputStream fis = new FileInputStream("1.txt");
FileOutputStream fos = new FileOutputStream("2.txt");
//读入的通道
FileChannel fisChannel = fis.getChannel();
FileChannel fosChannel = fos.getChannel();
//缓冲区
ByteBuffer bf = ByteBuffer.allocate(1024);
while (fisChannel.read(bf) != -1) {
//开启读模式
bf.flip();
fosChannel.write(bf);
//每写完一次清空缓冲区
bf.clear();
}
* 或者通过 通道的静态方法 open()打开
* FileChannel inchannel = FileChannel.open(Paths.get("1.txt"), StandardOpenOption.READ);
FileChannel outchannel = FileChannel.open(Paths.get("4.txt"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
* 或者通过transferTo(),transferFrom()方法进行读写
* inchannel.transferTo(0, inchannel.size(), outchannel);