package java.nio.charset;
public abstract class Charset implements Comparable<Charset>{
private static volatile Charset defaultCharset;
/**
* Returns the default charset of this Java virtual machine.
*
* <p> The default charset is determined during virtual-machine startup and
* typically depends upon the locale and charset of the underlying
* operating system.
*
* @return A charset object for the default charset
*
* @since 1.5
*/
public static Charset defaultCharset() {
if (defaultCharset == null) {
synchronized (Charset.class) {
java.security.PrivilegedAction pa =
new GetPropertyAction("file.encoding");
String csn = (String)AccessController.doPrivileged(pa);
Charset cs = lookup(csn);
if (cs != null)
defaultCharset = cs;
else
defaultCharset = forName("UTF-8");
}
}
return defaultCharset;
}
}
就是一个lazy-init的单例模式实现。另外,其实就是取自file.encoding环境变量,取不到,默认是”UTF-8”。
Tips 可以在eclipse中用Display View中,执行java语句:
System.getProperty("file.encoding");
非常实用的功能。
YOU MIGHT ALSO LIKE
- 30 Aug 2026 » Python 在 AI-Infra:从语言机制到生产交付(总纲)
- 30 Aug 2026 » Python 在 AI-Infra(07):项目工程化与生产交付
- 29 Aug 2026 » Python 在 AI-Infra(06):单元测试、问题定位与调试实践
OUTLINE
没有匹配的标题