码迷,mamicode.com
首页 > 编程语言 > 详细

【第20章:Java新IO】_缓冲区与Buffer

时间:2016-09-30 23:38:11      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

import java.nio.IntBuffer ;
public class IntBufferDemo01{
    public static void main(String args[]){
        IntBuffer buf = IntBuffer.allocate(10) ;    // 准备出10个大小的缓冲区
        System.out.print("1、写入数据之前的position、limit和capacity:") ;
        System.out.println("position = " + buf.position() + ",limit = " + buf.limit() + ",capacty = " + buf.capacity()) ;
        int temp[] = {5,7,9} ;// 定义一个int数组
        buf.put(3) ;    // 设置一个数据
        buf.put(temp) ;    // 此时已经存放了四个记录
        System.out.print("2、写入数据之后的position、limit和capacity:") ;
        System.out.println("position = " + buf.position() + ",limit = " + buf.limit() + ",capacty = " + buf.capacity()) ;

        buf.flip() ;    // 重设缓冲区
        // postion = 0 ,limit = 原本position
        System.out.print("3、准备输出数据时的position、limit和capacity:") ;
        System.out.println("position = " + buf.position() + ",limit = " + buf.limit() + ",capacty = " + buf.capacity()) ;
        System.out.print("缓冲区中的内容:") ;
        while(buf.hasRemaining()){
            int x = buf.get() ;
            System.out.print(x + "、") ;
        }
    }
}


 

import java.nio.IntBuffer ;
public class IntBufferDemo02{
    public static void main(String args[]){
        IntBuffer buf = IntBuffer.allocate(10) ;    // 准备出10个大小的缓冲区
        IntBuffer sub = null ;    // 定义子缓冲区
        for(int i=0;i<10;i++){
            buf.put(2 * i + 1) ;    // 在主缓冲区中加入10个奇数
        }
        
        // 需要通过slice() 创建子缓冲区
        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()) ;
        System.out.print("主缓冲区中的内容:") ;
        while(buf.hasRemaining()){
            int x = buf.get() ;
            System.out.print(x + "、") ;
        }
    }
}

 

import java.nio.IntBuffer ;
public class IntBufferDemo03{
    public static void main(String args[]){
        IntBuffer buf = IntBuffer.allocate(10) ;    // 准备出10个大小的缓冲区
        IntBuffer read = null ;    // 定义子缓冲区
        for(int i=0;i<10;i++){
            buf.put(2 * i + 1) ;    // 在主缓冲区中加入10个奇数
        }
        read = buf.asReadOnlyBuffer()  ;// 创建只读缓冲区
        


        read.flip() ;    // 重设缓冲区
        System.out.print("主缓冲区中的内容:") ;
        while(read.hasRemaining()){
            int x = read.get() ;
            System.out.print(x + "、") ;
        }
        read.put(30) ;    // 修改,错误
    }
}

 //ByteBuffer    系统优化比较速度快点

import java.nio.ByteBuffer ;
public class ByteBufferDemo01{
    public static void main(String args[]){
        ByteBuffer buf = ByteBuffer.allocateDirect(10) ;    // 准备出10个大小的缓冲区
        byte temp[] = {1,3,5,7,9} ;    // 设置内容
        buf.put(temp) ;    // 设置一组内容
        buf.flip() ;

        System.out.print("主缓冲区中的内容:") ;
        while(buf.hasRemaining()){
            int x = buf.get() ;
            System.out.print(x + "、") ;
        }
    }
}
 

【第20章:Java新IO】_缓冲区与Buffer

标签:

原文地址:http://www.cnblogs.com/wangminlomt5/p/5924852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!