32

I have an issue.

I have a properties file. I want to store some values in that file and will implement in the code whenever it is required. Is there any way to do that?

I am using Properties class to do that..

Kumar V
  • 8,810
  • 9
  • 39
  • 58
user3411418
  • 331
  • 1
  • 3
  • 6
  • Have you looked at the Javadoc for the `Properties` class? – Dawood ibn Kareem Mar 13 '14 at 05:49
  • 2
    Possible duplicate of [how to update property value in properties file using java?](http://stackoverflow.com/questions/15337409/how-to-update-property-value-in-properties-file-using-java) – Zeeshan Mar 13 '14 at 05:54
  • There are _Files_ (on the file system) and there are _resources_ on the class path. For writing you should opt for the file system, not in the application jar. – Joop Eggen Jun 30 '18 at 17:08

4 Answers4

45

Load the properties file using java.util.Properties.

Code snippet -

Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("xyz.properties");
prop.load(in);

It provides Properties#setProperty(java.lang.String, java.lang.String) which helps to add new property.

Code snippet -

prop.setProperty("newkey", "newvalue");

This new set you can save using Properties#store(java.io.OutputStream, java.lang.String)

Code Snippet -

prop.store(new FileOutputStream("xyz.properties"), null);
Neuron
  • 5,141
  • 5
  • 38
  • 59
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
11

You can do it in following way:

  1. Set the properties first in the Properties object by using object.setProperty(String obj1, String obj2).

  2. Then write it to your File by passing a FileOutputStream to properties_object.store(FileOutputStream, String).

Here is the example code :

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;

class Main
{
    static File file;
    static void saveProperties(Properties p) throws IOException
    {
        FileOutputStream fr = new FileOutputStream(file);
        p.store(fr, "Properties");
        fr.close();
        System.out.println("After saving properties: " + p);
    }

    static void loadProperties(Properties p)throws IOException
    {
        FileInputStream fi=new FileInputStream(file);
        p.load(fi);
        fi.close();
        System.out.println("After Loading properties: " + p);
    }

    public static void main(String... args)throws IOException
    {
        file = new File("property.dat");
        Properties table = new Properties();
        table.setProperty("Shivam","Bane");
        table.setProperty("CS","Maverick");
        System.out.println("Properties has been set in HashTable: " + table);
        // saving the properties in file
        saveProperties(table);
        // changing the property
        table.setProperty("Shivam", "Swagger");
        System.out.println("After the change in HashTable: " + table);
        // saving the properties in file
        saveProperties(table);
        // loading the saved properties
        loadProperties(table);
    }
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
Devavrata
  • 1,785
  • 17
  • 30
  • I am getting this error message : "Resource is out of sync with the file system: 'xyz.properties'." and also how to delete the file my job is over... And while retrieving the value the values are null...I am wondering what might be the issue? – user3411418 Mar 13 '14 at 08:19
  • For "Resources is out of syn" check [**link**](http://wiki.eclipse.org/FAQ_When_should_I_use_refreshLocal%3F).For deleting simple use delete function **file.delete()**. – Devavrata Mar 13 '14 at 15:10
  • this saves into the properties file but removes the existence data from file. – Sobhit Sharma Oct 16 '19 at 10:11
3

Your problem is not clear since Writing/reading from properties files is something already available in java.

To write to properties file you can use the Properties class as you mentioned :

Properties properties = new Properties();
try(OutputStream outputStream = new FileOutputStream(PROPERTIES_FILE_PATH)){  
    properties.setProperty("prop1", "Value1");
    properties.setProperty("prop2", "Value2");
    properties.store(outputStream, null);
} catch (IOException e) {
    e.printStackTrace();
} 

Source and more examples here

Mehdi
  • 1,340
  • 15
  • 23
2

This work for me.

    Properties prop = new Properties();

                try {
                        InputStream in = new FileInputStream("src/loop.properties");
                        prop.load(in);
                    } catch (IOException ex) {
                       System.out.println(ex);
                    }
           //Setting the value to  our properties file.
           prop.setProperty("LOOP", "1");
           //Getting the value from  our properties file.
           String value = prop.getProperty("LOOP").trim();

                try {
                        prop.store(new FileOutputStream("src/loop.properties"), null);
                    } catch (IOException ex) {
                       System.out.println(ex);
                    }
VyTcdc
  • 986
  • 10
  • 32