Using a ByteBuffer to Store Strings - Java File Path IO

Java examples for File Path IO:ByteBuffer

Description

Using a ByteBuffer to Store Strings

Demo Code

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

public class Main {
  public static void main(String[] args) throws Exception {
    ByteBuffer buf = ByteBuffer.allocate(100);

    CharBuffer cbuf = buf.asCharBuffer();

    cbuf.put("a string");

    cbuf.flip();//from  w w  w .  j av a 2s.  c o  m
    String s = cbuf.toString(); // a string

    // Get a substring
    int start = 2; // start is relative to cbuf's current position
    int end = 5;
    CharSequence sub = cbuf.subSequence(start, end); // str
  }
}

Related Tutorials