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() + ",");
}
}以上就是java中Buffer的概念,希望对大家有所帮助。 更多Java学习指路: Java基础
本教程操作环境:windows7系统、java10版,DELL G3电脑。
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did255030