Setting the default Java character encoding

后端 未结 17 1749
终归单人心
终归单人心 2020-11-21 06:09

How do I properly set the default character encoding used by the JVM (1.5.x) programmatically?

I have read that -Dfile.encoding=whatever used to be the

17条回答
  •  终归单人心
    2020-11-21 06:46

    I have a hacky way that definitely works!!

    System.setProperty("file.encoding","UTF-8");
    Field charset = Charset.class.getDeclaredField("defaultCharset");
    charset.setAccessible(true);
    charset.set(null,null);
    

    This way you are going to trick JVM which would think that charset is not set and make it to set it again to UTF-8, on runtime!

提交回复
热议问题