我想改变.properties里属性的值,但是为啥还是原来的值呢,求高手解答啊

woniu4500 2012-11-19 05:13:16
以下是我写的重置值的函数,谢谢
public void setPara(String fileName,String key,String value) {
Properties prop = new Properties();
try{
ClassLoader cl = this.getClass().getClassLoader();
InputStream is = cl.getResourceAsStream(fileName);
prop.load(is);
if(is != null)
is.close();
}catch(Exception e){
System.out.println(e + "file " + fileName + " not found");
}
prop.setProperty(key,value);
FileOutputStream fos;
try {
fos = new FileOutputStream(fileName);
try {
prop.store(fos, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
...全文
275 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
suciver 2012-11-19
  • 打赏
  • 举报
回复
你读取的是class目录下的config.properties可是你在set方法里面把它输出到了项目的目录下了 要把那个set方法的 FileOutputStream fos = new FileOutputStream(cl.getResource(fileName).toURI().getPath());改成这样
woniu4500 2012-11-19
  • 打赏
  • 举报
回复
什么原因呢?
woniu4500 2012-11-19
  • 打赏
  • 举报
回复
public class ReadPro implements Serializable { public String getPara(String fileName,String field){ Properties prop = new Properties(); try{ ClassLoader cl = this.getClass().getClassLoader(); InputStream is = cl.getResourceAsStream(fileName); prop.load(is); if(is != null) is.close(); }catch(Exception e){ System.out.println(e + "file " + fileName + " not found"); } return prop.getProperty(field); } public void setPara(String fileName,String key,String value) { Properties prop = new Properties(); try{ ClassLoader cl = this.getClass().getClassLoader(); InputStream is = cl.getResourceAsStream(fileName); prop.load(is); if(is != null) is.close(); }catch(Exception e){ System.out.println(e + "file " + fileName + " not found"); } prop.setProperty(key,value); FileOutputStream fos; try { fos = new FileOutputStream(fileName); try { prop.store(fos,null); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String [] args){ ReadPro readpro = new ReadPro(); String ex = readpro.getPara("config.properties", "ExceptionPiont"); System.out.println("++++++++++++++++++++++"+ex); readpro.setPara("config.properties","ExceptionPiont" , "23"); String exchange = readpro.getPara("config.properties", "ExceptionPiont"); System.out.println("++++++++++++++++++++++"+exchange); } }

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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