java propertes 文件读取乱码问题
方法1
使用native2ascii 把中文转化为ascii码,这样读取的使用不会乱码。
这种方式不推荐。
方式2
Properties prop = new Properties();
prop.load(is);
关键是第二行代码,这里如果你传的是
?InputStream is= Test.class.getResourceAsStream(".sunline");
就会出现乱码。
如果你在包装一层。
InputStreamReader reader= new InputStreamReader(is);
把InputStreamReader 传给Properties ?
prop.load(reader);这样就不会乱码了。
?
因为InputStreamReader 是支持可以直接读取字符串的。?
?
so
public class PropertiesFactory {
public static Properties createProperties(InputStream is) {
Properties prop = new Properties();
InputStreamReader reader = new InputStreamReader(is);
try {
prop.load(reader);
return prop;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
?
?