Note that the only reliable way I've found to write characters to the console using the using is to use the PrintStream Java sets up for System.out and System.err. Because in Java we should be using Writer for characters, I wrote a wrapper PrintStreamWriter which maintains the underlying PrintStream charset (which is impossible to get without playing reflection tricks), unlike PrintWriter (which forces you to specify a charset, which as discussed we don't know, which defeats the purpose). However if the user redirects either one of these (e.g. to a file) from the command line, the output sent to the file will still get what the console charset would have been, not the system default charset. I don't know if it's possible to detect whether redirection is occurring. As mentioned above in Java 17+ we can see whether Console.charset() returns null, but that doesn't tell us which of the streams is being redirected (and moreover makes it impossible to find the charset of the other). It's a mess. |