Java运行时字符编码 file.encoding
- 2016-04-18 17:30:00
- admin
- 原创 2004
一、Java运行时字符编码
public static void printEncoding() {
Map<String, Charset> charsets = Charset.availableCharsets();
for (Map.Entry<String, Charset> entry: charsets.entrySet()) {
String key = entry.getKey();
Charset charset = entry.getValue();
if (key.equals("GBK")) {
System.out.println(charset.name());
System.out.println(charset.displayName());
System.out.println(charset.aliases());
} else if (key.equals("UTF-8")) {
System.out.println(charset.name());
System.out.println(charset.displayName());
System.out.println(charset.aliases());
}
}
String encoding1 = System.getProperty("file.encoding");
System.out.println("Encoding: " + encoding1);
String encoding2 = Charset.defaultCharset().name();
System.out.println("Encoding: " + encoding2);
}
windows默认编码:
GBK
GBK
[CP936, windows-936]
UTF-8
UTF-8
[UTF8, unicode-1-1-utf-8]
Encoding: GBK
Encoding: GBK
windows指定编码,-Dfile.encoding=UTF-8:
GBK
GBK
[CP936, windows-936]
UTF-8
UTF-8
[UTF8, unicode-1-1-utf-8]
Encoding: UTF-8
Encoding: UTF-8
linux默认编码:
GBK
GBK
[windows-936, CP936]
UTF-8
UTF-8
[unicode-1-1-utf-8, UTF8]
Encoding: UTF-8
Encoding: UTF-8