Java supports six character encodings: - Java Internationalization

Java examples for Internationalization:Charset

Introduction

Set Description
US-ASCII 128-character ASCII
ISO-8859-1 256-character ISO Latin Alphabet No. 1 character set
UTF-8includes US-ASCII and the Universal Character Set (Unicode)
UTF-16BE 16-bit characters with bytes stored in big-endian byte order
UTF-16LE 16-bit characters with bytes stored in little-endian byte order
UTF-16 16-bit characters with the order of bytes indicated by an optional byte-order mark

The following statement creates a Charset object for the ISO-8859-1 character set:

Charset isoset = Charset.forName ("ISO-8859-1"); 

The following statements convert a byte buffer called netBuffer into a character buffer using the ISO-8859-1 character set:

Demo Code

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

public class Main {
  public static void main(String[] arguments) throws Exception{
    ByteBuffer netBuffer = ByteBuffer.allocate(20480);
    // code to fill byte buffer would be here
    Charset set = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = set.newDecoder();
    netBuffer.position(0);//from   www .j  a v a2s  .c  o m
    CharBuffer netText = decoder.decode(netBuffer);

  }

}

Related Tutorials