zoukankan      html  css  js  c++  java
  • 1-NIO使用

    1.FileChannel 和 Buffer

    package nio._Buffer;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.nio.Buffer;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    public class Demo {
        public static void main(String[] args) throws IOException {
            RandomAccessFile aFile = new RandomAccessFile("f:/zhuopeng/nioFile.txt","rw");
            FileChannel fileChannel = aFile.getChannel();
    
    
            RandomAccessFile aFile2 = new RandomAccessFile("f:/zhuopeng/nioFile2.txt","rw");
            FileChannel fileChannel2 = aFile2.getChannel();
    
            //分配内存,创建一个buffer,capacity为48
            ByteBuffer buf = ByteBuffer.allocate(48);
    
            //1.放入缓存的方法,从channel读取
            int read = fileChannel.read(buf);
    
            //2.放入缓存的方法,自己手动放进去
            String str = "zhuopeng";
            byte[] b = str.getBytes();
            buf.put(b);
    
            while (read != -1) {
                //切换为读模式
                buf.flip();
                //标记当前的position
                buf.mark();
                while (buf.hasRemaining()){
                    System.out.print((char)buf.get());//一次读取一个byte
                }
                //重新回到当前的position
                buf.reset();
                //将buf中的内容写到另一个channel中
                int write = fileChannel2.write(buf);
    
                buf.clear();
                read = fileChannel.read(buf);
            }
            aFile.close();
    
        }
    }

     2. Scattering 和 Gathering,以下是Scatter,Gather和Scatter差不多

    package nio._Buffer;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    public class _ScatterAndGather {
        public static void main(String[] args) throws IOException {
    
            
            RandomAccessFile file1 = new RandomAccessFile("f:/zhuopeng/nioFile.txt","rw");
            
            FileChannel fileChannel = file1.getChannel();
    
            //构造两个buffer,并且加入到一个数组中
            ByteBuffer buf1 = ByteBuffer.allocate(5);
            ByteBuffer buf2 = ByteBuffer.allocate(20);
            ByteBuffer[] bufArr = {buf1,buf2};
    
            long read = fileChannel.read(bufArr);
    
            while(read != -1){
                buf1.flip();
                buf2.flip();
                //读取第一个buf
                System.out.println("第一个buf结果:");
                while (buf1.hasRemaining()){
                    System.out.print((char)buf1.get());//一次读取一个byte
                }
                System.out.println("");
                System.out.println("第二个buf结果显示:");
                while (buf2.hasRemaining()){
                    System.out.print((char)buf2.get());
                }
    
                
                read = -1;
            }
    
    
    
    
        }
    }

    2.Channel Transfer

    public class _ChannelTransfer {
        public static void main(String[] args) throws IOException {
            RandomAccessFile file1 = new RandomAccessFile("nioFile.txt", "rw");
            RandomAccessFile file2 = new RandomAccessFile("nioFile3.txt", "rw");
    
            FileChannel fileChannel1 = file1.getChannel();
            FileChannel fileChannel2 = file2.getChannel();
            long size = fileChannel1.size();
    
            fileChannel2.transferFrom(fileChannel1,0,size);
            fileChannel1.close();
            fileChannel2.close();
            file1.close();
            file2.close();
        }
    }

    4.Selector

  • 相关阅读:
    Spring Cloud Gateway配置自定义异常返回
    C#开机启动,托盘图标等小功能
    微信内置浏览器搞事情之调试模式
    物联网架构成长之路(56)-SpringCloudGateway+JWT实现网关鉴权
    物联网架构成长之路(55)-Gateway+Sentinel实现限流、熔断
    物联网架构成长之路(53)-Sentinel流量控制中间件入门
    物联网架构成长之路(54)-基于Nacos+Gateway实现动态路由
    物联网架构成长之路(52)-基于Nacos+prometheus+grafana的监控
    物联网架构成长之路(51)-Nacos微服务配置中心、服务注册服务发现
    物联网架构成长之路(50)-EMQ配置SSL证书,实现MQTTs协议
  • 原文地址:https://www.cnblogs.com/da-peng/p/10092430.html
Copyright © 2011-2022 走看看