public static String getLabel(String status){
Properties properties = new Properties();
try {
//文件被放在classes的根路径下
InputStream in =PropertyUtil.class.getClassLoader().getResourceAsStream("statuslable.properties");
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
String statuskey = "status"+status;
String label = null;
try {
//property中的中文进行转码处理
label = new String (properties.getProperty(statuskey).getBytes("ISO-8859-1"),"GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return label;
一般来说,ResourceBundle类通常是用于针对不同的语言来使用的属性文件。
而如果你的应用程序中的属性文件只是一些配置,并不是针对多国语言的目的。那么使用Properties类就可以了。
通常可以把这些属性文件放在某个jar文件中。然后,通过调用class的getResourceAsStream方法,来获得该属性文件的流对象,再用Properties类的load方法来装载。
示例如下:
Class TestLoad {
public static void main( String[] argv) {
InputStream is = TestLoad.class.getResourceAsSteam("myprops.properties");
Properties p = new Properties();
p.load(is);
System.out.println(p.get("MAIL_SERVER_HOSTNAME"));
}
}
}