// 读
public String readPeopleNumber(){
String peopleNumber="";
Properties prop = new Properties();
try{
//读取属性文件tuancheData.properties
String path = ServletActionContext.getRequest().getRealPath("/data/tuancheData.properties");
InputStream in = new BufferedInputStream (new FileInputStream(path));
prop.load(in); ///加载属性列表
Iterator it=prop.stringPropertyNames().iterator();
while(it.hasNext()){
String key=it.next();
System.out.println(key+":"+prop.getProperty(key));
peopleNumber = prop.getProperty(key);
}
in.close();
}
catch(Exception e){
System.out.println(e);
}
return peopleNumber;
}
// 写
public void writePeopleNumber(String number) throws Exception{
Properties prop = new Properties();// 属性集合对象
String path = ServletActionContext.getRequest().getRealPath("/data/tuancheData.properties");
//FileInputStream fis = new FileInputStream(path);// 属性文件输入流
//prop.load(fis);// 将属性文件流装载到Properties对象中
//fis.close();// 关闭流
// 获取属性值,sitename已在文件中定义
//System.out.println("获取属性值:peopleNumber=" + prop.getProperty("peopleNumber"));
// 获取属性值,country未在文件中定义,将在此程序中返回一个默认值,但并不修改属性文件
// System.out.println("获取属性值:country=" + prop.getProperty("country", "中国"));
// 修改sitename的属性值
prop.setProperty("peopleNumber", number);
// 文件输出流
FileOutputStream fos = new FileOutputStream(path);
// 将Properties集合保存到流中
prop.store(fos, "Copyright (c) Boxcode Studio");
fos.close();// 关闭流
//System.out.println("获取修改后的属性值:peopleNumber=" + prop.getProperty("peopleNumber"));
}