This section provides a tutorial example on how to display a list character encodings supported by JDK with the java.nio.charset.Charset.availableCharsets() method.
JDK uses the java.nio.charset.Charset class to represent a character encoding,
with both encode() method and decode() method.
It also provides a method, availableCharsets(), to return all supported encodings.
Here is a program to display all the supported character encodings in JDK:
/**
* Encodings.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.nio.charset.*;
import java.util.*;
class Encodings {
public static void main(String[] arg) {
SortedMap m = Charset.availableCharsets();
Set k = m.keySet();
System.out.println("Canonical name, Display name,"
+" Can encode, Aliases");
Iterator i = k.iterator();
while (i.hasNext()) {
String n = (String) i.next();
Charset e = (Charset) m.get(n);
String d = e.displayName();
boolean c = e.canEncode();
System.out.print(n+", "+d+", "+c);
Set s = e.aliases();
Iterator j = s.iterator();
while (j.hasNext()) {
String a = (String) j.next();
System.out.print(", "+a);
}
System.out.println("");
}
}
}