简体   繁体   中英

How to write values in a properties file through java code

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..

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM