I have a program I run with mvn exec:java (my main file is encoded in utf-8 and the default charset of my system is windows-1252)
System.out.println(Charset.defaultCharset()); //print windows-1252
String s = "éàè";
System.out.println(new String(s.getBytes(Charset.forName("UTF-8")))); //OK Print éàè
System.out.println(new String(s.getBytes(Charset.forName("windows-1252")))); //Not OK Print ▒▒▒
I don't understand why the first print works, according to the documentation getBytes encodes the String into a sequence of bytes using the given charset and the String constructor constructs a new String by decoding the specified array of bytes using the platform's default charset
So the first print encodes in UTF-8 and then decode with the platform's default charset which is windows-1252, how could this workd ? It cannot decode the encoded utf-8 byte array using the platform charset windows-1252.
The second print is wrong, I don't understand why. As my file is encoded in utf-8 and the platform charset is windows-1252, my intention is to encode the String with windows-1252 charset so I call s.getBytes(Charset.forName("windows-1252")) and then create a String with the previous result but it doesn't work
PrintStream out = new PrintStream(System.out, true, "windows-1252"); out.println(s);