RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Jun 8, 2021

Working with Charset in Java

Java supports multiple charsets, thanks to the library java.nio.charset.Charset. We will be using this library in today's programming tutorial to illustrate how to find out if UTF-8 and UTF-558 charsets are supported or not.

Below you will find out how to tell if charsets are supported using Java:


import java.nio.charset.Charset.*;
import java.util.*;

public class SupportedCharsets

	public static String charsetToFind = "UTF-8";
	public static void main(String args[])
		SupportedCharsets supportedCharsets = new SupportedCharsets();
	private void proceed()
		SortedMap sortedMap = java.nio.charset.Charset.availableCharsets();
		Iterator iterator = sortedMap.keySet().iterator();
		String key   = null;
		Object value = null;
		while(iterator.hasNext()) {
			key   = (String) iterator.next();
			if (key.equalsIgnoreCase(charsetToFind))
				value = sortedMap.get(key);	
				System.out.println(key + "," + value);
		if (value == null)
				System.out.println(charsetToFind + " not found.");


When we run this Java code, we should get the following output:

[root@mypc]# java SupportedCharsets

If you replace the value in charsetToFind to "UTF-558" and then execute the code, the output will change to:

[root@mypc]# java SupportedCharsets
UTF-558 not found.
MS Sridhar
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date