Charset Canonical Name And Aliases Lookup
Charset.forName accepts case-insensitive aliases while each charset keeps one canonical name; availableCharsets lists every registered charset.
Code
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
ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.ASCII85 EncoderEncode and decode ASCII85 (Base85), the Adobe variant with z zero-compression, handling arbitrary bytes and Unicode.Base32 Encoder & DecoderEncode and decode RFC 4648 Base32 with padding, fully UTF-8 safe so Unicode text round-trips without corruption. Nothing leaves your browser.Base45 Encoder and DecoderEncode and decode text with the Base45 alphabet defined in RFC 9285, with UTF-8 support, padding-free output, and clear validation errors.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.