How does Java determine the encoding used for System.out
?
Given the following class:
import java.io.File;
import java.io.PrintWriter;
public class Foo
{
public static void main(String[] args) throws Exception
{
String s = "xx??xx";
System.out.println(s);
PrintWriter out = new PrintWriter(new File("test.txt"), "UTF-8");
out.println(s);
out.close();
}
}
It is saved as UTF-8 and compiled with javac -encoding UTF-8 Foo.java
on a Windows system.
Afterwards on a git-bash console (using UTF-8 charset) I do:
$ java Foo
xx?±xx
$ java -Dfile.encoding=UTF-8 Foo
xx├?├?xx
$ cat test.txt
xx??xx
$ java Foo | cat
xx??xx
$ java -Dfile.encoding=UTF-8 Foo | cat
xx??xx
What is going on here?
Obviously java checks if it is connected to a terminal and is changing its encoding in that case. Is there a way to force Java to simply output plain UTF-8?
I tried the same with the cmd console, too. Redirecting STDOUT does not seem to make any difference there. Without the file.encoding parameter it outputs ansi encoding with the parameter it outputs utf8 encoding.
See Question&Answers more detail:os