The output of the following program differs depending on whether I have IntelliJ compile it, or whether I compile it by hand from the command line. As far as I can tell, I have only one compiler and JVM on my machine.
public class OutputTest {
public static void main(String[] args) throws Throwable {
System.out.println(" Java Version: " +
System.getProperty("java.version") +
" from " + System.getProperty("java.vendor"));
String message = "Dźien Dobry. Jak się masz?";
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Charset charset = Charset.forName("UTF-8");
PrintWriter pw = new PrintWriter(new java.io.BufferedWriter(
new java.io.OutputStreamWriter(stream, charset)), true);
pw.write(message);
pw.close();
System.out.println(stream.toString());
System.out.println(java.util.Arrays.toString(stream.toByteArray()));
}
}
Output when compiled in IntelliJ:
Java Version: 1.6.0_26 from Apple Inc.
Dźien Dobry. Jak się masz?
[68, -59, -70, 105, 101, 110, 32, 68, 111, 98, 114, 121, 46, 32, 74, 97, 107, 32, 115, 105, -60, -103, 32, 109, 97, 115, 122, 63]
Output when compiled from the command line:
Java Version: 1.6.0_26 from Apple Inc.
D≈∫ien Dobry. Jak siƒô masz?
[68, -30, -119, -120, -30, -120, -85, 105, 101, 110, 32, 68, 111, 98, 114, 121, 46, 32, 74, 97, 107, 32, 115, 105, -58, -110, -61, -76, 32, 109, 97, 115, 122, 63]
To be clear: The output depends on where the code is compiled. I also see the first answer if I have IntelliJ compile the code, but run it from the command line.
I am running MacOS 10.6.8.
Also, for what it’s worth: When I compile it from the command line in Linux, I get the same answer that the IntelliJ-compiled version gives.
Check the platform encoding; you may need to specify the -encoding parameter on the command line to match what IntelliJ is doing. Without it it’ll use the default platform encoding, which may or may not match how IntelliJ behaves.