zoukankan      html  css  js  c++  java
  • Java——新IO 缓冲区与Buffer

    缓冲区和Buffer

    import java.nio.IntBuffer;
    
    //=================================================
    // File Name       :	IntBuffer_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    
    //主类
    //Function        : 	IntBuffer_demo
    public class IntBuffer_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		IntBuffer buf = IntBuffer.allocate(10);			//开辟10个大小的缓冲区
    		System.out.print("1.写入数据之前的position、limit和capacity");
    		System.out.println("position="+buf.position()+"、limit="+buf.limit()+"、capacity="+buf.capacity());
    		int temp[] = {3,5,7};					//定义整型数组
    		buf.put(3);									//向缓冲区写入数据
    		buf.put(temp);							//向缓冲区中写入一组数据
    		System.out.print("2.写入数据之后的position、limit和capacity");
    		System.out.println("position="+buf.position()+"、limit="+buf.limit()+"、capacity="+buf.capacity());
    		buf.flip();						//重设缓冲区,改变指针
    		System.out.print("3.准备输出数据时的position、limit和capacity");
    		System.out.println("position="+buf.position()+"、limit="+buf.limit()+"、capacity="+buf.capacity());
    		while(buf.hasRemaining()){
    			int x = buf.get();
    			System.out.print(x+"、");
    		}
    	}
    
    }
    

     

     

    创建子缓冲区

    import java.nio.IntBuffer;
    
    //=================================================
    // File Name       :	IntBuffer_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    
    //主类
    //Function        : 	IntBuffer_demo
    public class IntBuffer_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		
    		IntBuffer buf = IntBuffer.allocate(10);			//开辟10个大小的缓冲区
    		IntBuffer sub = null;											//定义缓冲区对象
    		for(int i=0;i<10;i++){
    			buf.put(2*i+1);
    		}
    		buf.position(2);
    		buf.limit(6);
    		sub = buf.slice();				//开辟子缓冲区
    		for(int i=0;i<sub.capacity();i++){
    			int temp = sub.get(i);
    			sub.put(temp-1);
    		}
    		buf.flip();			//重设缓冲区
    		buf.limit(buf.capacity());	//设置limit
    		System.out.println("主缓冲区中的内容:");
    		while(buf.hasRemaining()){
    			int x = buf.get();					//取出当前内容
    			System.out.print(x+"、");
    		}
    	}
    
    }
    

     

    import java.nio.IntBuffer;
    
    //=================================================
    // File Name       :	IntBuffer_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    
    //主类
    //Function        : 	IntBuffer_demo
    public class IntBuffer_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		
    		IntBuffer buf = IntBuffer.allocate(10);			//开辟10个大小的缓冲区
    		IntBuffer read = null;											//定义缓冲区对象
    		for(int i=0;i<10;i++){
    			buf.put(2*i+1);
    		}
    		read = buf.asReadOnlyBuffer();			//创建只读缓冲区
    		buf.flip();			//重设缓冲区
    		System.out.println("主缓冲区中的内容:");
    		while(buf.hasRemaining()){
    			int x = buf.get();					//取出当前内容
    			System.out.print(x+"、");
    		}
    		System.out.println();
    		read.put(30);			//错误,不可写
    	}
    
    }
    

     

  • 相关阅读:
    Winform中多线程无法访问使用 Control.CheckForIllegalCrossThreadCalls = false;
    PV操作-生产者/消费者关系
    table表格长度超出屏幕范围,可滑动
    Koa2中间件计算响应总耗时/设置响应头/读取Json文件返回给客户端
    Koa2简介和搭建
    计算机浮点数的表示和运算
    CSS实现Loading加载中动画
    RPC
    Git常用命令
    如何解决 shell 脚本重复执行的问题
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5324181.html
Copyright © 2011-2022 走看看