0

I have a property file having different data. I need to update the value of a property. How can achieve this using Java?

My property file contains:

# Module Password for Installer
MODULE_PASSWORD=Mg==

# Modules Selected for Installer
ausphur=yes
einfuhr=no
edec=no
emcs=no
ncts=no
suma=no
eas=no
zollager=no

I need to change yes or no values.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
WSDL
  • 35
  • 9

4 Answers4

0

You could use Properties Java class.
It allows you to handle in a very easy way properties files based on (key, value) pair.
Have a look at http://docs.oracle.com/javase/tutorial/essential/environment/properties.html
In particular:

Saving Properties

The following example writes out the application properties from the previous example using Properties.store. The >default properties don't need to be saved each time because they never change.

FileOutputStream out = new FileOutputStream("appProperties");
applicationProps.store(out, "---No Comment---");
out.close();
The store method needs a stream to write to, as well as a string that it uses as a comment at the top of the output.

seph
  • 118
  • 1
  • 5
0

Try this.
Properties prop = new Properties(); OutputStream output = null;

        output = new FileOutputStream("config.properties");

        // set the properties value
        prop.setProperty("propertyname", "newValue");
        prop.store(output, null);
Shriram
  • 4,343
  • 8
  • 37
  • 64
0

Create a FileOutputStream to the properties file and modify the property by using Properties.setProperty(PROPERTY_NAME,PROPERTY_VALUE) and then call store(out,null) on Properties instance

        FileOutputStream out = new FileOutputStream("config.properties");
        props.setProperty("ausphur", "no");
        props.store(out, null);
        out.close();

This will help!

Update:

There is no way to keep your comments back since Properties doesn't know about it. If you use java.util.Properties, it is a subclass of Hashtable which doesn't preserve the order of the keys and values the way they are inserted. What you can do is, you can go for LinkedHashMap collection with your own implementation of Properties datastore. But, you have to read the properties from file yourself and put it in LinkedHashMap. To Note, LinkedHashMap preserves the order in which keys are values are put. so, you can iterate the keyset and update the properties file in the same order back. Thus, the order can be preserved

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • no.File content gets changed.but its order changing and some comments are missing – WSDL Mar 13 '14 at 03:56
  • it would be helpful for me if u could find the answer – WSDL Mar 13 '14 at 04:27
  • I don't think the comments will be preserved since the file is rewritten with properties from memory. The order of the properties is also not preserved. i'm still confused with your expected output.Please clarify – Keerthivasan Mar 13 '14 at 05:04
  • you are right. The order of the property is not preserved.I need that to be preserved. – WSDL Mar 13 '14 at 08:09
  • Please try `LinkedHashMap` with your own implementation – Keerthivasan Mar 13 '14 at 09:12
  • plz show me an example code – WSDL Mar 13 '14 at 11:10
  • PleaseRefer this [link](http://stackoverflow.com/questions/3619796/how-to-read-a-properties-file-in-java-in-the-original-order) – Keerthivasan Mar 13 '14 at 12:28
  • @Keerthivasan What if you have delimiters? ausphur = yes@!no@!no@!yes. Then how would you add a new value of "no" at the end of the current value? – JayC Dec 12 '16 at 18:44
0

Use java.util.Properties.

    Properties props = new Properties();
    FileInputStream fis = new FileInputStream("pathToYourFile");
    props.load(fis);
    fis.close();
    props.setProperty("propName", "propValue");
    FileOutputStream fos = new FileOutputStream("pathToYourFile");
    props.store(fos, "Your comments");
    fos.close();
yavuzkavus
  • 1,268
  • 11
  • 17