Mega Code Archive

 
Categories / Java / File Input Output
 

Using a ByteBuffer to Store Strings

import java.nio.ByteBuffer; import java.nio.CharBuffer; public class Main {   public static void main(String[] argv) throws Exception {     ByteBuffer buf = ByteBuffer.allocate(100);     CharBuffer cbuf = buf.asCharBuffer();     cbuf.put("a string");     cbuf.flip();     String s = cbuf.toString(); // a string   } }