NIO的三大核心配件
文章插图
channel(通道)
Buffer(缓冲区)
selector(选择器)
案例介绍读写切换
public static void main(String[] args) {//创建一个Buffer,大小为5,可就是可以存放5个int IntBuffer intBuffer=IntBuffer.allocate(5); for(int i=0;i<intBuffer.capacity();i++){intBuffer.put(i*2); }//读写切换 intBuffer.flip(); while(intBuffer.hasRemaining()){System.out.println(intBuffer.get()); }}
本地方法写public static void main(String[] args) throws IOException { String str="hello"; //创建一个输出流 FileOutputStream fileOutputStream=new FileOutputStream("src/test/JAVA/com/hua/nio/a"); //通过fileOutputStream获取对应的FileChannel,这个FielChannel的真实类型是FileChannelImpl FileChannel fileChannel=fileOutputStream.getChannel(); //创建一个缓冲区ByteBuffer ByteBuffer byteBuffer=ByteBuffer.allocate(1024); //将str放到byteBuffer byteBuffer.put(str.getBytes()); //对byteBuffer.flip(); byteBuffer.flip(); //把byteBuffer的数据写入到fileChannel fileChannel.write(byteBuffer); fileOutputStream.close();}
本地方法读public static void main(String[] args) throws IOException {File file=new File("src/test/java/com/hua/nio/a"); FileInputStream fileInputStream =new FileInputStream(file); FileChannel fileChannel=fileInputStream.getChannel(); ByteBuffer byteBuffer= ByteBuffer.allocate((int)file.length()); //将通道的数据读入到Buffer fileChannel.read(byteBuffer); //将byteBuffer的字节数据转成String System.out.println(new String(byteBuffer.array())); fileInputStream.close();}
数据的双向交互public static void main(String[] args) throws IOException {FileInputStream fileInputStream=new FileInputStream("src/test/java/com/hua/nio/a"); FileOutputStream fileOutputStream=new FileOutputStream("src/test/java/com/hua/nio/b"); FileChannel fileChannel1=fileInputStream.getChannel(); FileChannel fileChannel2=fileOutputStream.getChannel(); ByteBuffer byteBuffer= ByteBuffer.allocate(512); while(true){//必须要clear,当下面fileChannel2的write的时候,此时的position会从0开始到limit,如果不clear回归到原来,//当position和limit相等的时候,fileChannel1.read的返回结果为-1 byteBuffer.clear(); int read=fileChannel1.read(byteBuffer); System.out.println(read); if(read == -1){break; }byteBuffer.flip(); fileChannel2.write(byteBuffer); }fileInputStream.close(); fileOutputStream.close();}
通道的数据转换public static void main(String[] args) throws IOException {FileInputStream fileInputStream =new FileInputStream("src/test/java/com/hua/nio/a"); FileOutputStream fileOutputStream=new FileOutputStream("src/test/java/com/hua/nio/c"); FileChannel fileChannel1=fileInputStream.getChannel(); FileChannel fileChannel2=fileOutputStream.getChannel(); fileChannel2.transferFrom(fileChannel1,0,fileChannel1.size()); fileChannel1.close(); fileChannel2.close(); fileInputStream.close(); fileOutputStream.close();}
类型的bufferpublic static void main(String[] args) {ByteBuffer buffer= ByteBuffer.allocate(64); buffer.putInt(100); buffer.putLong(5); buffer.putChar('天'); buffer.putShort((short)11); buffer.flip(); System.out.println(); System.out.println(buffer.getInt()); System.out.println(buffer.getLong()); System.out.println(buffer.getChar()); System.out.println(buffer.getShort());}
只读的bufferpublic static void main(String[] args) {ByteBuffer buffer= ByteBuffer.allocate(64); for(int i=0;i<64;i++){buffer.put((byte)i); }buffer.flip(); //设置byteBuffer是只读的 ByteBuffer readOnlyBuffer=buffer.asReadOnlyBuffer(); System.out.println(readOnlyBuffer.getClass()); while(readOnlyBuffer.hasRemaining()){System.out.println(readOnlyBuffer.get()); }}
内存上的修改public static void main(String[] args) throws IOException {RandomaccessFile randomAccessFile=new RandomAccessFile("src/test/java/com/hua/nio/a","rw"); FileChannel channel=randomAccessFile.getChannel(); //FileChannel.MapMode.READ_WRITR使用的是读写模式 //第一个参数表示直接修改的起始位置 //第二个参数表示映射到内存的大小 //也就是说我们只能修改0到4的索引位置,MAppedByteBuffer mappedByteBuffer=channel.map(FileChannel.MapMode.READ_WRITE,0,5); mappedByteBuffer.put(0,(byte)'H'); mappedByteBuffer.put(2,(byte)'L'); randomAccessFile.close();}
【Java NIO的三大核心配件:通道、缓冲区、选择器】
推荐阅读
- 推荐好听的水果茶名字,水果茶的制作方法
- 白茶的来源,云南白茶和福鼎白茶的区别
- 域名HTTPS证书怎么弄
- 有志者当效此生是什么意思?
- 曹操说过的经典名句有哪些?
- 人生感悟的句子悲伤?伤感的话看了会哭短句关于人生
- 保持轻松愉快的心情?保持心情快乐只有一个方法
- 乾陵石像生?乾陵的61个石人头找到了吗
- 人间温柔的经典诗句?很温柔的诗句
- 白砂糖和蔗糖有什么区别,白砂糖的主要成分是蔗糖吗