Java Charset Example

By Arvind Rai, November 18, 2023
Java Charset plays the role of encoding and decoding between given charset and UNICODE. Charset name must begin with a letter or number. It has a canonical name as an alias. Charset methods are safe to use in multithreading environment.

Standard Charsets

The supported Charset in java are given below.
US-ASCII: Seven bit ASCII characters.
ISO-8859-1: ISO Latin alphabet
UTF-8: This is 8 bit UCS transformation format.
UTF-16BE: This is 16 bit UCS transformation format with big endian byte order
UTF-16LE: This is 16 bit UCS transformation with little endian byte order.
UTF-16: 16 bit UCS transformation format.

Static Methods

forName() : Creates a charset object for the given charset name. The name can be canonical or an alias.
displayName() : Returns canonical name of charset.
canEncode() : Checks whether the given charset supports encoding or not.
decode() : Decodes the string of a given charset into charbuffer of Unicode charset.
encode() : Encodes charbuffer of unicode charset into the byte buffer of given charset.

Example

CharsetExample.java
package com.concretepage.nio.charset;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class CharsetExample {
	public static void main(String[] args) {
		 Charset charset=Charset.forName("US-ASCII");
		 System.out.println(charset.displayName());
		 System.out.println(charset.canEncode());
		 String s= "Hello, This is Charset Example.";
		 //convert byte buffer in given charset to char buffer in unicode
		 ByteBuffer bb = ByteBuffer.wrap(s.getBytes());
		 CharBuffer cb = charset.decode(bb);
		//convert char buffer in unicode to byte buffer in given charset
		 ByteBuffer newbb = charset.encode(cb);
		 while(newbb.hasRemaining()){
	        	char ch = (char) newbb.get();
	        	System.out.print(ch);
	     }
		 newbb.clear();
	}
}  
In the example, we have taken a charset canonical name as US-ASCII and we are changing a string into char buffer of Unicode charset by using byte buffer with US-ASCII charset and again changing that char buffer of Unicode charset into byte buffer of given charset.

Output

US-ASCII
Hello, This is Charset Example.
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us