Java String convert from ByteBuffer by Charset
import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; public class Main { public static void main(String[] argv) { ByteBuffer b = ByteBuffer.wrap("demo2s.com".getBytes()); // w w w . j a v a2s . c om String s = stringFromBuffer(b , Charset.defaultCharset()); System.out.println(s); } /** * Convert the content of a byte buffer to a string * * @param buf * is the byte buffer * @param charset * indicates the character set encoding * @return a string */ public static String stringFromBuffer(ByteBuffer buf, Charset charset) { CharBuffer cb = charset.decode(buf.duplicate()); return (cb.toString()); } }