UTF-8将Java String编码为Properties

mar*_*ers 11 java io properties utf-8

我有一个UTF-8编码的字符串,它是一串键+值对,需要加载到Properties对象中.我注意到我的初始实现时出现乱码,经过一些谷歌搜索后,我发现这个问题表明我的问题是什么 - 基本上默认使用ISO-8859-1属性.这个实现看起来像

public Properties load(String propertiesString) {
        Properties properties = new Properties();
        try {
            properties.load(new ByteArrayInputStream(propertiesString.getBytes()));
        } catch (IOException e) {
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return properties;
    }
Run Code Online (Sandbox Code Playgroud)

没有指定编码,因此我的问题.对于我的问题,我无法弄清楚如何链接/创建一个Reader/ InputStream组合传递给Properties.load()使用提供的propertiesString并指定编码.我认为这主要是由于我在I/O流方面缺乏经验以及java.io包中看似庞大的IO实用程序库.

任何建议表示赞赏

Mat*_*all 13

使用Reader字符串时使用.InputStreams真的是用于二进制数据.

public Properties load(String propertiesString) {
    Properties properties = new Properties();
    properties.load(new StringReader(propertiesString));
    return properties;
}
Run Code Online (Sandbox Code Playgroud)