public class BytebufferTest{
public static void main(String[] args)throws IOException{
ByteBuffer bbuffer = ByteBuffer.allocate(10);
byte[] b1 = new byte[]{'a','b'};
bbuffer.put(b1);
System.out.println(bbuffer.position());
char[] c1 = new char[]{'c','d'};
bbuffer.asCharBuffer().put(c1);
System.out.println(bbuffer.position());
buffer.rewind();
while(bbuffer.hasRemaining()){
System.out.println((char)bbuffer.get());
}
}
}
请问为什么“System.out.println(bbuffer.position());”输出的position值一样,之后的“'c','d'”不是已经成功存入byterbuffer了吗,为什么position值不相应增加?
谢谢大侠!
[b]问题补充:[/b]
TO:RednaxelaFX
请问大侠这段JavaDoc您是在哪里找到的?我也想拜读下。
[quote="Javadoc"]The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; [color=red]the two buffers' position, limit, and mark values will be independent[/color].[/quote]
因为asCharBuffer()之后对返回出来的CharBuffer调用put()不会改变原来的ByteBuffer的position,虽然内容是联动的
在这里:[url]http://java.sun.com/javase/6/docs/api/java/nio/ByteBuffer.html[/url]
如果你需要查文档的时候总是能连上网,那就在官网查在线版javadoc就好。要离线看的话,在[url=http://java.sun.com/javase/downloads/index.jsp]这里[/url]下载Java SE 6 Documentation。下好文档随便找个地方解开,Eclipse和Netbeans就支持自动显示文档的。