How to use UTF-8 in resource properties with ResourceBundle

后端 未结 16 2226
难免孤独
难免孤独 2020-11-22 03:28

I need to use UTF-8 in my resource properties using Java\'s ResourceBundle. When I enter the text directly into the properties file, it displays as mojibake.

16条回答
  •  礼貌的吻别
    2020-11-22 03:42

    look at this : http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#load(java.io.Reader)

    the properties accept an Reader object as arguments, which you can create from an InputStream.

    at the create time, you can specify the encoding of the Reader:

    InputStreamReader isr = new InputStreamReader(stream, "UTF-8");
    

    then apply this Reader to the load method :

    prop.load(isr);
    

    BTW: get the stream from .properties file :

     InputStream stream = this.class.getClassLoader().getResourceAsStream("a.properties");
    

    BTW: get resource bundle from InputStreamReader:

    ResourceBundle rb = new PropertyResourceBundle(isr);
    

    hope this can help you !

提交回复
热议问题