How to get console encoding?

How to get the console (cmd.exe in windows, linux shell or eclipse console output) encoding encoding? java.nio.charset.Charset.defaultCharset()appears only for input / output files, not for the console.

+5
source share
4 answers

In general: you will need to ask the shell what encoding it uses to display characters.

Guess without knowing: there is no standard method in Java (I think) there is no standard for consoles to report the actual encoding. We will need to determine the actual operating system or console provider (eclipse, ...) and use their specific functions to get the name of the actual encoding.

0
source

. , , , ( , ).

โ€‹โ€‹ , , Java. OpenJDK, , :

final Class<? extends PrintStream> stdOutClass = System.out.getClass();
final Field charOutField = stdOutClass.getDeclaredField("charOut");
charOutField.setAccessible(true);
OutputStreamWriter o = (OutputStreamWriter) charOutField.get(System.out);
System.out.println(o.getEncoding());

UTF8 [sic] , , UTF-8 Linux.

+2

JDK 1.1 OutputStreamWriter System out getEncoding().

OutputStreamWriter osw = new OutputStreamWriter(System.out);
System.out.println(osw.getEncoding());
+2

You might want to ask your IDE if you use it. If you do not, then it will be used regardless of your shell. If you use eclipse, it will be the same as your โ€œproject character setโ€, which you can find / modify by right-clicking on your project, properties and resource โ†’ text file encoding

0
source

All Articles