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);			//错误,不可写
    	}
    
    }
    

     

  • 相关阅读:
    Android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据(转)
    保存图片文件到本地
    android ScrollView中嵌套GridView,ListView只显示一行的解决办法
    蒙版提示页(添加新功能后的一种提示)
    C和指针 第三章--数据
    *(ptr++) += 123
    优先级队列-堆
    单链表相关(一)
    字符间的距离-动态规划
    和最大的连续子数组
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5324181.html
Copyright © 2011-2022 走看看