package zwf.xml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Configuration{
private Properties propertie;
private String path;
private FileInputStream inputFile;
private FileOutputStream outputFile;
public Configuration(){
propertie = new Properties();
}
public Configuration(String filePath){
path = filePath;
propertie = new Properties();
try {
inputFile = new FileInputStream(filePath);
propertie.load(inputFile);
inputFile.close();
} catch (FileNotFoundException ex){
System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
ex.printStackTrace();
} catch (IOException ex){
System.out.println("装载文件--->失败!");
ex.printStackTrace();
}
}
public String getValue(String password){
if(propertie.containsKey(password)){
String value = propertie.getProperty(password);//得到某一属性的值
return value;
}
else
return "";
}
public void clear(){
propertie.clear();
}
public void setValue(String password, String value) throws IOException{
propertie.setProperty(password, value);
FileOutputStream fos = new FileOutputStream("D:/myjweb/workspace/cxxue/src/zwf/xml/test.properties");
propertie.store(fos, "Copyright (c) Boxcode Studio");
fos.close();
}
public void saveFile(String description){
try {
outputFile = new FileOutputStream(path);
propertie.store(outputFile, description);
outputFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
public static void main(String[] args) {
Configuration rc = new Configuration("D:/myjweb/workspace/cxxue/src/zwf/xml/test.properties");
//测试从文件读取
String password = rc.getValue("password");
System.out.println("password = " + password);
String t = rc.getValue("test");
System.out.println("t = " + t);
//测试保存saveFile
try {
rc.setValue(password, t);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("password = " + password);
}
}
我想在jsp页面调用这个类实现对properties文件的读取与修改 其中只有一对建值password=28525,我想在jsp页面实现读取和修改,get方法已经实现了,就是说zaijsp页面我可以调用getvalue方法读取到password的值,但是如何才能实现修改呢?
------解决方案--------------------
楼主大好人,,接分咯。