UTF-8编码的Java字符串转换为 Properties

2020-04-25  本文已影响0人  黑曼巴yk

问题

通过properties.load方法读取properties的数据,如果出现中文则会乱码

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;
    }

原因

InputStream其实是二进制数据,使用Reader接口替代

public Properties load(String propertiesString) {
    Properties properties = new Properties();
    properties.load(new StringReader(propertiesString));
    return properties;
}
上一篇 下一篇

猜你喜欢

热点阅读