java 为 Charset.forName(String) 编码 CharsetNames

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12557208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:22:01  来源:igfitidea点击:

Encoding CharsetNames for Charset.forName(String)

javacharacter-encoding

提问by Jason Ching

I have a question about Charset.forName(String charsetName). Is there a list of charsetNames I can refer to? For example, for UTF-8, we use "utf8" for the charsetName. What about WINDOWS-1252, GB18030, etc.?

我有一个关于 Charset.forName(String charsetName) 的问题。是否有我可以参考的 charsetNames 列表?例如,对于 UTF-8,我们使用“utf8”作为字符集名称。WINDOWS-1252、GB18030等呢?

回答by iBabur

Charset         Description

US-ASCII        Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
ISO-8859-1      ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
UTF-8           Eight-bit UCS Transformation Format
UTF-16BE        Sixteen-bit UCS Transformation Format, big-endian byte order
UTF-16LE        Sixteen-bit UCS Transformation Format, little-endian byte order
UTF-16          Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark

Reference: http://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html

参考:http: //docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html

回答by telebog

The charset names in Java are platform dependent, there are only 6 constants in the StandardCharsetsclass.

Java 中的字符集名称是平台相关的,StandardCharsets类中只有 6 个常量。

To view the all charsets you should look at IANA. Check Preferred MIME Name and aliases columns.

要查看所有字符集,您应该查看IANA。检查首选 MIME 名称和别名列。

回答by GSFK

To list all character set installed in your JVM, you might use the following code snippet (Java 8 SE or higher):

要列出 JVM 中安装的所有字符集,您可以使用以下代码片段(Java 8 SE 或更高版本):

SortedMap<String, Charset> map = Charset.availableCharsets();
map.keySet().stream().forEach(System.out::println);

On my system, this lists around 170 character sets.

在我的系统上,这列出了大约 170 个字符集。

回答by alexis

The java Charset library is requiredto accept just a few basic encodings: ASCII, Latin-1 (ISO-8859-1), and a handful of UTF variants that you can see listed in this answer. That's a pretty useless list for any practical purposes, unless your scope is limited to Latin-1. In reality, Java classes can handle a large number of encodings that you can read about in the Supported Encodingspage. Quoting from it:

java Charset 库只需要接受一些基本编码:ASCII、Latin-1 (ISO-8859-1) 和一些您可以在此答案中看到的 UTF 变体。对于任何实际目的来说,这是一个非常无用的列表,除非您的范围仅限于 Latin-1。实际上,Java 类可以处理大量编码,您可以在Supported Encodings页面中了解这些编码。引用它:

The java.io.InputStreamReader, java.io.OutputStreamWriter, java.lang.Stringclasses, and classes in the java.nio.charsetpackage can convert between Unicode and a number of other character encodings. The supported encodings vary between different implementations of Java SE 8. The class description for java.nio.charset.Charsetlists the encodings that any implementation of Java SE 8 is required to support.

JDK 8 for all platforms (Solaris, Linux, and Microsoft Windows) and JRE 8 for Solaris and Linux support all encodings shown on this page.JRE 8 for Microsoft Windows may be installed as a complete international version or as a European languages version. [...]

包中的java.io.InputStreamReaderjava.io.OutputStreamWriterjava.lang.String类和类java.nio.charset可以在 Unicode 和许多其他字符编码之间进行转换。支持的编码因 Java SE 8 的不同实现而异。 的类描述java.nio.charset.Charset列出了 Java SE 8 的任何实现都需要支持的编码。

适用于所有平台(Solaris、Linux 和 Microsoft Windows)的 JDK 8 和适用于 Solaris 和 Linux 的 JRE 8 支持此页面上显示的所有编码。JRE 8 for Microsoft Windows 可以作为完整的国际版本或欧洲语言版本安装。[...]

The rest of the pageconsists of an extensive table of encoding names and synonyms, which is what the OP was after all those years ago...

页面的其余部分包含一个广泛的编码名称和同义词表,这就是 OP 多年前的样子......