Java examples for Internationalization:Charset
Converting Between Strings (Unicode) and Other Character Set Encodings
import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetEncoder; public class Main { public static void main(String[] argv) throws Exception { Charset charset = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = charset.newDecoder(); CharsetEncoder encoder = charset.newEncoder(); try {/* www. java 2 s. c o m*/ ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("a string")); CharBuffer cbuf = decoder.decode(bbuf); String s = cbuf.toString(); } catch (CharacterCodingException e) { } ByteBuffer bbuf = ByteBuffer.allocateDirect(1024); // Create a non-direct character ByteBuffer CharBuffer cbuf = CharBuffer.allocate(1024); // Convert characters in cbuf to bbuf encoder.encode(cbuf, bbuf, false); // flip bbuf before reading from it bbuf.flip(); // Convert bytes in bbuf to cbuf decoder.decode(bbuf, cbuf, false); // flip cbuf before reading from it cbuf.flip(); } }