Charset Canonical Name And Aliases Lookup

Charset.forName accepts case-insensitive aliases while each charset keeps one canonical name; availableCharsets lists every registered charset.

JavaData Integrity

Code

snippet
import java.nio.charset.Charset;
import java.util.Map;

public class CharsetAliases {

    public static void main(String[] args) {
        // forName 不区分大小写:别名(any alias)都会解析到唯一的 canonical name
        for (String name : new String[] {"UTF-8", "utf8", "unicode-1-1-utf-8"}) {
            Charset charset = Charset.forName(name);
            System.out.println(name + " -> canonical " + charset.name()
                    + " aliases " + charset.aliases());
        }

        // availableCharsets:以 canonical 名为键、按名字排序的只读 map
        Map<String, Charset> registered = Charset.availableCharsets();
        System.out.println("ISO-8859-1 registered: " + registered.containsKey("ISO-8859-1"));
        System.out.println("UTF-16 aliases: " + registered.get("UTF-16").aliases());
    }
}

Where the work happens

The code’s whole job belongs to the class CharsetAliases and the function main.

The loop for (String name : new String[] {"UTF-8", "utf8", "unicode-1-1-utf-8"}) is the only repeated construct.

Watch out for

  • All symbols resolve to language built-ins — nothing gets fetched.

Using it

Signature: public static void main(String[] args)

The demo output is labeled (ISO-8859-1 registered:, UTF-16 aliases:); each label names the call that printed it.

Key statements

Map<String, Charset> registered = Charset.availableCharsets();
Charset charset = Charset.forName(name);

Interactive Tools for This Topic

FAQ

What does Charset Canonical Name And Aliases Lookup print when run?
Invoke the file as-is — the `ISO-8859-1 registered:`, `UTF-16 aliases:` prefixes identify which step printed each line.