我有一个单一的UTF-8编码的字符串,它是一系列键值对,需要加载到Properties对象中.我注意到我正在使用我的初步实现的乱码,经过一番谷歌搜索,我发现这个
Question表明我的问题是 – 基本上是默认使用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;
}
没有指定的编码,因此我的问题.对于我的问题,我不知道如何链接/创建一个Reader / InputStream组合,以传递给使用提供的propertiesstring并指定编码的Properties.load().我认为这主要是因为我在I / O流中的缺乏经验以及java.io包中看似庞大的IO实用程序库.
任何建议赞赏.
解决方法
使用字符串时使用
Reader.
InputStreams真的是二进制数据.
public Properties load(String propertiesstring) {
Properties properties = new Properties();
properties.load(new StringReader(propertiesstring));
return properties;
}