美文网首页
UTF-8编码的Java字符串转换为 Properties

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

作者: 黑曼巴yk | 来源:发表于2020-04-25 00:30 被阅读0次

问题

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

相关文章

网友评论

      本文标题:UTF-8编码的Java字符串转换为 Properties

      本文链接:https://www.haomeiwen.com/subject/wjbcwhtx.html