你应该像这样使用反射 API。
PrintStream ps = new PrintStream(System.out,true,StandardCharsets.UTF_16);
Field f = ps.getClass().getDeclaredField("charOut");
f.setAccessible(true);
OutputStreamWriter charOut = (OutputStreamWriter) f.get(ps);
System.out.println(charOut.getEncoding());
输出:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by stackoverflow.AAA field java.io.PrintStream.charOut
WARNING: Please consider reporting this to the maintainers of stackoverflow.AAA
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
UTF-16
您需要注意最后一条警告信息。
我在 Eclipse 调试器中找到了字段 charOut
。
本文链接:https://www.f2er.com/697875.html