properties 使用问题

虽不简单 2011-09-18 02:06:43
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的值,但是如何才能实现修改呢?
...全文
52 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yexiongMYBH 2011-09-18
  • 打赏
  • 举报
回复
楼主大好人,,接分咯。
虽不简单 2011-09-18
  • 打赏
  • 举报
回复
谁来给谁分!
虽不简单 2011-09-18
  • 打赏
  • 举报
回复
问题已解决!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧