learning java NIO 之 CharBuffer

import java.nio.CharBuffer;

public class BufferTest {
    public static void main(String[] args) {
        CharBuffer  buff = CharBuffer.allocate(8);
        System.out.println("capacity: " + buff.capacity());
        System.out.println("limit: " + buff.limit());;
        System.out.println("position:" + buff.position());

        buff.put('a');
        buff.put('b');
        buff.put('c');
        System.out.println("position:" + buff.position());

        buff.flip();
        System.out.println("limit: " + buff.limit());;
        System.out.println("position:" + buff.position());

        System.out.println(buff.get());
        System.out.println(buff.position());

        buff.clear();
        System.out.println("limit: " + buff.limit());;
        System.out.println("position:" + buff.position());

        System.out.println(buff.get(2));
        System.out.println("position:" + buff.position());



    }
}

output:

capacity: 8
limit: 8
position:0
position:3
limit: 3
position:0
a
1
limit: 8
position:0
c
position:0

 

posted @ 2019-08-06 08:40  嵌入式实操  阅读(1147)  评论(0编辑  收藏  举报