读取properties 的方式
通过Properties类来获取:
public Properties properties;
try {
File file=new File(pathname)
properties = new Properties();
properties.load(new BufferedInputStream(fis));
String svalue=properties.getProperty("key");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
通过ResourceBundle 类来读取:
ResourceBundle rbd = ResourceBundle.getBundle("myproperties",Locale.getDefault());
String str = rbd.getString("key");
try {
System.out.println(str);
System.out.println(new String(str.getBytes("ISO-8859-1"),"GB2312"));//如果遇到中文乱码问题可这样转换
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
当使用ResourceBundle 读取properties 时 properties的文件必须在classpath 下,不然会报错,这个需要注意