ITeye博客

java.util.Properties类使用注意

zttcc · 2008-07-17 22:46 · Java · Markdown

这样写可以就可以避免重新覆盖:

FileInputStream fin = new FileInputStream(file);

config.load(fin);
FileOutputStream fout=new FileOutputStream(file);

 

java.util.Properties类每次写文件时是采取覆盖的方式进行的,未提供方法单独修改其中的某一个属性

使用时一定要注意,必须将原先的所有值全部读取,再重新写回去。

例:

写道

配置文件:property.txt
格式:
[list]
name=1
age=12
sex=male
[/list]

修改其中一个配置:
eg. age=100

最后的property.txt为:
[list]
name=1
age=100
sex=male
[/list]

请问大侠怎么做,最好能给个简单的代码看看,谢谢

 

 

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。