更新属性文件中的属性值而不删除其他值
 First.properties内容: 
 name=elango country=india phone=12345 
 我想从india到america改变country 。 这是我的代码: 
 import java.io.*; public class UpdateProperty { public static void main(String args[]) throws Exception { FileOutputStream out = new FileOutputStream("First.properties"); FileInputStream in = new FileInputStream("First.properties"); Properties props = new Properties(); props.load(in); in.close(); props.setProperty("country", "america"); props.store(out, null); out.close(); } } 
  First.properties输出内容: 
 country=america 
其他属性被删除。 我想更新一个特定的属性值,而不删除其他属性。
closuresinputstream后,打开输出stream并存储属性。
 FileInputStream in = new FileInputStream("First.properties"); Properties props = new Properties(); props.load(in); in.close(); FileOutputStream out = new FileOutputStream("First.properties"); props.setProperty("country", "america"); props.store(out, null); out.close(); 
您可以使用Apache Commonsconfiguration库。 最好的部分,如果是这样,它甚至不会搞乱属性文件,并保持完好( 甚至评论 )。
的Javadoc
 PropertiesConfiguration conf = new PropertiesConfiguration("propFile.properties"); props.setProperty("key", "value"); conf.save(); 
 Properties prop = new Properties(); prop.load(...); // FileInputStream prop.setProperty("key", "value"); prop.store(...); // FileOutputStream