集册 Java实例教程 使用ByteBuffer存储字符串

使用ByteBuffer存储字符串

欢马劈雪     最近更新时间:2020-01-02 10:19:05

520
使用ByteBuffer存储字符串

import java.nio.ByteBuffer;

import java.nio.CharBuffer;


public class Main {//来自 n  o  w  j  a  v  a . c o m

  public static void main(String[] args) throws Exception {

    ByteBuffer buf = ByteBuffer.allocate(100);


    CharBuffer cbuf = buf.asCharBuffer();


    cbuf.put("a string");


    cbuf.flip();

    String s = cbuf.toString(); // a string
/*时 代 J a v a - N o w J a v a . c o m 提供*/

    // Get a substring

    int start = 2; // start is relative to cbuf's current position

    int end = 5;

    CharSequence sub = cbuf.subSequence(start, end); // str

  }

}