Skip to content

Instantly share code, notes, and snippets.

@sebgod
Last active August 29, 2015 14:03
Show Gist options
  • Save sebgod/9aa655d75e287d9d7e00 to your computer and use it in GitHub Desktop.
Save sebgod/9aa655d75e287d9d7e00 to your computer and use it in GitHub Desktop.
Testing UTF-8 as a default output for Windows
class Hello {
public static boolean ensureEncodingToUTF8() {
String osEncoding = System.getProperty("file.encoding");
if (!osEncoding.equals("UTF-8") || !osEncoding.equals("UTF8")) {
java.lang.System.setProperty("file.encoding", "UTF-8");
try {
System.setOut(new java.io.PrintStream(System.out, true, "UTF-8"));
} catch (Exception inner) {
return false;
}
}
return true;
}
public static void main(String[] args) {
ensureEncodingToUTF8();
System.out.println("你好世界!");
}
}
javac -J-Dfile.encoding=UTF-8 Hello.java && jar cvfe hello.jar Hello Hello.class && jar i hello.jar && java -jar hello.jar
#!/bin/sh
javac Hello.java && jar cvfe hello.jar Hello Hello.class && jar i hello.jar && java -jar hello.jar
class UnicodePrintStream extends java.io.PrintStream {
public UnicodePrintStream(java.io.OutputStream orig,
java.nio.charset.Charset charset)
throws java.io.UnsupportedEncodingException {
super(orig, true, charset.name());
}
public java.io.OutputStream getOriginalStream() {
return out;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment