Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating property value in properties file without deleting other values [duplicate]

Content of First.properties:

name=elango country=india phone=12345 

I want change country from india to america. This is my code:

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();     }  } 

Output content of First.properties:

country=america 

The other properties are deleted. I want update a particular property value, without deleting the other properties.

like image 851
Elangovan Avatar asked Mar 11 '13 11:03

Elangovan


People also ask

How set value in properties file in Java?

To set properties in a Java Properties instance you use the setProperty() method. Here is an example of setting a property (key - value pair) in a Java Properties object: properties.


1 Answers

Open the output stream and store properties after you have closed the input 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(); 
like image 90
Vasyl Keretsman Avatar answered Sep 28 '22 05:09

Vasyl Keretsman