信息网站建设费使用年限,app推广拉新,深圳app外包公司排行榜,wordpress安卓版怎么用Java NIO
1.BIO与NIO的区别
BIO为阻塞IO#xff0c;NIO为非阻塞IO。
BIONIOJAVA1.4之前Java 1.4之后面向流#xff1a;以byte为单位处理数据面向块#xff1a;以块为单位处理数据同步阻塞同步非阻塞无选择器#xff08;Selector#xff09;
1.1NIO的核心组成部分
Cha…Java NIO
1.BIO与NIO的区别
BIO为阻塞IONIO为非阻塞IO。
BIONIOJAVA1.4之前Java 1.4之后面向流以byte为单位处理数据面向块以块为单位处理数据同步阻塞同步非阻塞无选择器Selector
1.1NIO的核心组成部分
Channels Channel是双向的既能做读操作也能做写操作常见Channel如下
Channel类功能FileChannel文件数据读写DtagramChannelUDP数据读写ServerScoketChannel和SocketChannelTCP数据读写
Buffers 缓冲区Selectors 选择器用于监听多个通道的事件可实现单个线程就可以监听多个客户端通道。
2.Channel
Channel封装了对数据源的操作可以操作多种数据源但是不必关心数据源的具体物理结构。Channel用于在字节缓冲区和另一侧的实体之间有效地传输数据。 Channel所有数据都是通过Buffer对象进行处理通道要么读数据到缓冲区要么从缓冲区写入到通道。 public interface Channle extend Closeable {public boolean isOpen();public void close() throws IOException;
}2.1 FileChannel
FileChannel常用方法如下;
方法名作用public int read(ByteBuffer dst)从通道读取数据并放到缓冲区中public int write(ByteBuffer src)把缓冲区的数据写到通道中public long transferFrom(ReadableByteChannel src, long position, long count)从目标通道中复制数据到当前通道public long transferTo(long position, long count, WritableByteChannel target)把数据从当前通道复制给目标通道
无法直接打开一个FileChannel常见的方法是通过inPutStream和outPutStream或RandomAccessFile获取一个FileChannel实例。 示例代码 文件写入示例
package com.hero.nio.file;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
//通过NIO实现文件IO
public class TestNIO {
Test //往本地文件中写数据
public void test1() throws Exception{//1. 创建输出流FileOutputStream fosnew FileOutputStream(basic.txt);//2. 从流中得到一个通道FileChannel fcfos.getChannel();//3. 提供一个缓冲区ByteBuffer bufferByteBuffer.allocate(1024);//4. 往缓冲区中存入数据String strHelloJava;buffer.put(str.getBytes());//5. 翻转缓冲区buffer.flip();while(buffer.hasRemaining)) {//6. 把缓冲区写到通道中fc.write(buffer);}//7. 关闭fos.close();}
}文件复制示例
public void test4() throws Exception {//1. 创建两个流FileInputStream fis new FileInputStream(basic2.txt);FileOutputStream fos new FileOutputStream(basic3.txt);//2. 得到两个通道FileChannel sourceFC fis.getChannel();FileChannel destFC fos.getChannel();//3. 复制destFC.transferFrom(sourceFC, 0, sourceFC.size());//4. 关闭fis.close();fos.close();
}