1

I am unable to write into my properties file for not sure why as not encountering any kind of error or something. I have tried debugging but nothing found. Here is my PropertiesUtil class.

public class PropertyUtil {

    public static PropertyUtil prop;
    private Properties properties;

    public PropertyUtil() {
        properties = new Properties();
    }

    public static synchronized PropertyUtil getInstance() {
        if (prop == null) {
            prop = new PropertyUtil();
        }
        return prop;
    }

    public void load(String fileName) throws IOException {
        InputStream input = null;
        input = getClass().getClassLoader().getResourceAsStream(fileName);
        properties.load(input);
    }

    public void load(File file) throws IOException {
        InputStream input = new FileInputStream(file);
        properties.load(input);
    }

    public void clear() {
        properties.clear();
    }

    public String getValue(String key) {
        return properties.getProperty(key).trim();
    }

    public void setValue(String key, String value) {
        properties.setProperty(key, value);
    }

    public HashMap<String, String> propertiesMap() {
        HashMap<String, String> map = new HashMap<String, String>((Map) properties);
        return map;
    }

    public void propertytonull() {
        prop = null;
    }
}

And this is how I am trying to write into the properties file in class.

prop.setValue("RequestId",res_RequestId); prop.setValue("Id",res_Id);

RequestId and Id

is key already exists into my .properties file.

How to write values in a properties file through java code is not the answer to my question as answer in that file, removes all the data from the properties file and saves what you pass. Where as My question is verify specific to particular key.

Sobhit Sharma
  • 697
  • 14
  • 45
  • 8
    setProperty only set the value in your program. You need to save the file then – jhamon Oct 16 '19 at 07:10
  • 1
    I also see you are trying to implement the Singleton pattern, in this pattern your `PropertyUtil`constructor should be set to `private` so that no other instance of `PropertyUtil` can be instanciated and only one instance of `PropertyUtil` can be accessed via `getInstance`. – Saeleas Oct 16 '19 at 07:16
  • I tried this PropertyUtil.getInstance().setValue("RequestId",res_RequestId); PropertyUtil.getInstance().setValue("Id",res_Id); yet same. – Sobhit Sharma Oct 16 '19 at 07:47
  • did you read the linked answer? – jhamon Oct 16 '19 at 09:18
  • yes, but that didnt help either. Can you check my singleton class and tell me whats wrong here. – Sobhit Sharma Oct 16 '19 at 09:58
  • If you want to write property values into a file, how come you not have(/use) any write() or save() method ? – Erwin Smout Oct 16 '19 at 10:49

1 Answers1

0

This is how we can write into a property file. Also below code written for reading from the property file.

@Test  
public void momSave() {
        FileReader reader = null;
        try {
            reader = new FileReader(filepath/name.properties);
            Properties p = new Properties();
            p.load(reader);
            p.getProperty("RequestId"); // to read from the property file
            p.setProperty("RequestId","53403"); // to read into a property file
properties.store(filepath/name.properties),
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Tested and successfully working fine.

Sobhit Sharma
  • 697
  • 14
  • 45
  • This is not saving the changed property set to a file. Need to call one of `store()` methods that Properties provide. https://docs.oracle.com/javase/9/docs/api/java/util/Properties.html#store-java.io.OutputStream-java.lang.String- – david a. Oct 16 '19 at 11:45
  • yes right, I have updated my code. Thanks, I just missed copying it. – Sobhit Sharma Oct 16 '19 at 11:52