Reading from property file containing utf 8 character

前端 未结 4 1383
感动是毒
感动是毒 2020-12-14 17:58

I am reading a property file which consists of a message in the UTF-8 character set.

Problem

The output is not in the appropriate format.

相关标签:
4条回答
  • 2020-12-14 18:31

    Use props.load(new FileReader("uinsoaptest.properties")) instead. By default it uses the encoding Charset.forName(System.getProperty("file.encoding")) which can be set to UTF-8 with System.setProperty("file.encoding", "UTF-8") or with the commandline parameter -Dfile.encoding=UTF-8.

    0 讨论(0)
  • 2020-12-14 18:34

    Use an InputStreamReader with Properties.load(Reader reader):

    FileInputStream input = new FileInputStream(new File("uinsoaptest.properties"));
    props.load(new InputStreamReader(input, Charset.forName("UTF-8")));
    
    0 讨论(0)
  • 2020-12-14 18:39

    You should specify the UTF-8 encoding when you construct your FileInputStream object. You can use this constructor:

    new FileInputStream("uinsoaptest.properties", "UTF-8");
    

    If you want to make a change to your JVM so as to be able to read UTF-8 files by default, you will have to change the JAVA_TOOL_OPTIONS in your JVM options to something like this :

    -Dfile.encoding=UTF-8
    
    0 讨论(0)
  • 2020-12-14 18:44

    If somebody use @Value annotation, could try StringUils.

    @Value("${title}")
    private String pageTitle;
    
    public String getPageTitle() {
        return StringUtils.toEncodedString(pageTitle.getBytes(Charset.forName("ISO-8859-1")), Charset.forName("UTF-8"));
    }
    
    0 讨论(0)
提交回复
热议问题