资讯   |   开发   |   选机中心   |   产品大全 | IBM | 惠普 | 联想 | 戴尔 | 苹果 | 神舟
更多: | 华硕 | 明基 | 方正 | 紫光 | TCL | 夏新 | 联宝 | 宏碁 | 七喜 | 长城 | 清华同方 | 海尔 | 三星 | 东芝 | 索尼 | 富士通 | LG | 技术 | ddnoon
当前位置:笔记本 > 软件开发 >
Advertisement
文章正文

java.util.Properties类的使用

类型:转载   责任编辑:asp.net   日期:2007/05/23


热门软件下载:


   

已有一个配置文件:property.txt    
  格式:    
   
  name=1    
  age=12    
  sex=male    
   
   
  要修改其中一个配置:    
  eg.   age=100    
   
  希望得到的property.txt为:    
   
  name=1    
  age=100    
  sex=male    
   
   
  请问大侠怎么做,最好能给个简单的代码看看  
   
  这是我写的用于修改配置值的方法代码:    
   
  代码:    
   
  static   void   updateConfigureValue(String   targetFile,   String   parameterName,   String   parameterValue)  
  {    
          Properties   prop   =   new   Properties();    
              try   {    
              fis   =   new   FileInputStream(targetFile);    
                  fos   =   new   FileOutputStream(targetFile);    
                  prop.load(fis);    
                  prop.setProperty(parameterName,   parameterValue);    
   
                  prop.store(fos,   "Update   "   +   parameterName   +   "   value");    
                  }    
            catch   (IOException   e)   {    
                  System.out.println("Visit   "   +   targetFile   +   "   for   updating   "   +parameterName   +   "    
                                                          value   error");    
              }    
     
   
   
  问题是,由于fis和fos都指向了同一个文件,所以最后得到的property.txt成了这样的:    
   
   
  age=100    
   
  该怎么解决???

网友回答:

发表者:sirfei

prop.load(fis);   //仅仅是读取而已  
  prop.setProperty(parameterName,   parameterValue);   //只有set了age的parameterName,             parameterValue  
   
 

发表者:httruly

这是正确的

发表者:yz790724

用个循环,把别的原封不到的写进去。

发表者:jamesfancy

把   fos   =   new   FileOutputStream(targetFile);   放到   load   后面去。  
   
          public   static   void   updateConfigureValue(String   targetFile,  
                          String   parameterName,   String   parameterValue)   {  
                  Properties   prop   =   new   Properties();  
                  try   {  
                          FileInputStream   fis   =   new   FileInputStream(targetFile);  
                          prop.load(fis);  
                          fis.close();  
                          prop.setProperty(parameterName,   parameterValue);  
   
                          FileOutputStream   fos   =   new   FileOutputStream(targetFile);  
                          prop.store(fos,   "Update   "   +   parameterName   +   "   value");  
                          fos.close();  
                  }   catch   (IOException   e)   {  
                          System.out.println("Visit   "   +   targetFile   +   "   for   updating   "  
                                          +   parameterName   +   "value   error");  
                  }  
          }  
 

发表者:jamesfancy

原因,用   FileOutputStream   打开一个文件准备写入内容时,已经是新创建的一个文件,文件内容已经被清空,所以你读不出来东西。

发表者:javafaq2004

先读再写。你读的时候就准备写了。  
   
   
  fis   =   new   FileInputStream(targetFile);    
  fos   =   new   FileOutputStream(targetFile);


 

 
热门推荐笔记本: 新蓝笔记本
相关文章:
笔记本相关:
IT技术文章:
webmaster:popbb@126.com   最佳浏览:1024X768 MSIE
©2007 popbb.net All Rights Reserved