---
title: "java.util.Properties类使用注意"
author: "zttcc"
date: "2008-07-17T22:46:31Z"
tags: []
slug: "java-util-properties"
draft: false
lastmod: "2008-07-17T22:46:31Z"
id: 215965
url: "https://zttcc.iteye.com/blog/215965"
---

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

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");    
        		  } }
```