日期:2014-05-17 浏览次数:20943 次
public class GetProperty {
public static String readValue(String filePath,String key){
Properties props = new Properties();
try {
InputStream ips = new BufferedInputStream(new FileInputStream(filePath));
props.load(ips);
String value = props.getProperty(key);
System.out.println(key+"="+value);
return value;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
//读取全部信息
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
InputStream ips = new BufferedInputStream(new FileInputStream(filePath));
props.load(ips);
Enumeration enum = props.propertyNames();
while(enum.hasMoreElements()){
String key = (String)enum.nextElement();
String value = props.getProperty(key);
System.out.println(key+"="+value);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void writeProperties(String filePath,String paraKey,String paraValue){
Properties props = new Properties();
try {
OutputStream ops = new FileOutputStream(filePath);
props.setProperty(paraKey, paraValue);
props.store(ops, "set");
} catch (IOException e) {
e.printStackTrace();
}
}
}
------解决方案--------------------
觉得一般不太需要动态修改资源文件的内容,通常情况下它是配置好的,这样每次读取也都是相同的值。
动态修改的话,这样在用做按钮或菜单的国际化时,导致这次看是某值,下次看又是另一个值了,有点乱。
要改的话,也可以
Properties prop = new Properties();
try {
InputStream is= new FileInputStream(...);
prop.load(is);
OutputStream os= new FileOutputStream(...);
prop.setProperty(propertyName, propertyValue);
prop.store(os, "comments");
os.close();
is.close();
} catch (IOException e) {
}
------解决方案--------------------
配置数据如果要改的话,放到数据库里得啦,做成一个数据字典
------解决方案--------------------
不能动态的给,只能写死,如果了改了页面,那就要在配置文件里面作相应的改动
------解决方案--------------------
属性文件中用占位符{0},{1}等,可以实现动态!