public class NIODemo { public static void main(String[] args) { String str = "www.mldn.cn" ;//定义要保存到缓冲区的字符串 ByteBuffer buffer = ByteBuffer.allocate(20);//分配20个容量的缓冲区 System.out.println("1【开辟缓冲区】position="+buffer.position()+"、limit="+buffer.limit()+"、capacity="+buffer.capacity()); buffer.put(str.getBytes());//数据保存到缓冲区之中 System.out.println("2【写入缓冲区】position="+buffer.position()+"、limit="+buffer.limit()+"、capacity="+buffer.capacity()); buffer.flip();//重置缓冲区,才可以进行数据读取 System.out.println("3【重置缓冲区】position="+buffer.position()+"、limit="+buffer.limit()+"、capacity="+buffer.capacity()); while (buffer.hasRemaining()){//判断position与limit之间是否还有数据 System.out.print(buffer.get()+"、");//获取的是字节编码数据 } System.out.println("n4【缓冲区 *** 作完毕】position="+buffer.position()+"、limit="+buffer.limit()+"、capacity="+buffer.capacity()); buffer.clear();//清空缓冲区 System.out.println("5【清空缓冲区缓冲区】position="+buffer.position()+"、limit="+buffer.limit()+"、capacity="+buffer.capacity()); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)