❓
【Java】コンソール出力の文字化けを解消する方法
よく書いてある解決法は、-Dfile.encoding=UTF-8
とエンコードを指定する方法、IntelliJ IDEAの場合[Help]-[Edit Custom VM Options...]メニューでVMオプションファイルを開き、
-Dconsole.encoding=UTF-8
-Dfile.encoding=UTF-8
と書く(そして再起動する)ことです。
encoding - IntelliJ IDEA console issue - Stack Overflow
しかしこれでは不十分な事があります。
ある時自分が気づいたのは、標準出力では文字化けしてもログ出力では文字化けしない場合があるという事です。要するに、実はSystem.outのメソッドに問題があるということです。
以下のようにすれば解決します。
try {
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out), true, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new InternalError("VM does not support mandatory encoding UTF-8");
}
from: How can I change the Standard Out to "UTF-8" in Java - Stack Overflow
ロガーなど標準出力以外で文字化けする場合、それぞれ別個に対応する必要があります。
Discussion