商城首页欢迎来到中国正版软件门户

您的位置:首页 >java中Buffer的概念是什么

java中Buffer的概念是什么

  发布于2023-04-30 阅读(0)

扫一扫,手机访问

1、概念

使用Java NIO Buffers与NIO Channel交互。从Channel中读取数据到buffers里,从Buffer把数据写入到Channels;必须对Buffer的三个属性进行控制,即capacities能力、position-location、limit限制。

2、实例

public static void main(String[] args) {
        //生成一个长度为10的缓冲区
        IntBuffer intBuffer = IntBuffer.allocate(10);
        for (int i = 0; i < intBuffer.capacity(); ++i){
            int randomNum = new SecureRandom().nextInt(20);
            intBuffer.put(randomNum);
        }
        //状态翻转
        intBuffer.flip();
        while (intBuffer.hasRemaining()){
            //读取数据
            System.out.print(intBuffer.get() + ",");
        }
        //clear方法本质上并不是删除数据
        intBuffer.clear();
        System.out.print("\n");
        System.out.println("-----------------------------");
        while (intBuffer.hasRemaining()){
            System.out.print(intBuffer.get() + ",");
        }
    }
本文转载于:https://www.yisu.com/zixun/579181.html 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注