Marco A. Braghim
Marco A. Braghim

Reputation: 601

JAVA - Update .properties file

I'm trying to update my .properties file inside my jar, but without success. After try to do this in a thousand of different ways, I got this example here:

    final AbstractFileConfiguration prop = new PropertiesConfiguration("application.properties");

    System.out.println(prop.getProperty(property));

    if (prop.containsKey(property)) {
        prop.setProperty(property, value);
        prop.save();
    }

And this is returning for me an Exception:

org.apache.commons.configuration.ConfigurationException: Could not save to URL jar:file:/home/pedepano/myjarapp.jar!/application.properties
at org.apache.commons.configuration.AbstractFileConfiguration.save(AbstractFileConfiguration.java:464)
at org.apache.commons.configuration.AbstractFileConfiguration.save(AbstractFileConfiguration.java:377)
at com.library.core.Configs.updateSimpleProperty(Configs.java:151)
at com.print.model.business.caixa.cnab240.listmodel.AbstractCnab240ListModel.mouseClicked(AbstractCnab240ListModel.java:111)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6528)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)

Some say that's not possible to update the .properties file in runtime, some say that is. I have wasting really too time trying to find the answer... Is that possible? If yes, how? If no, what could I do instead?

Thank you very much!

Upvotes: 0

Views: 5192

Answers (2)

Meegh
Meegh

Reputation: 39

     Properties properties = new Properties();
     try {      
             String propertiesFilePath = ("your file path");
             FileInputStream fis = new FileInputStream(propertiesFilePath);
             properties.load(fis);

             properties.setProperty("key","value");

             FileOutputStream fos = new FileOutputStream(propertiesFilePath);
             properties.store(fos,null);
             System.out.println("SUCCESS");
     }
     catch(Exception e) {
             System.out.println("FAILED");
     }

Upvotes: 1

Matt Clark
Matt Clark

Reputation: 28579

You are trying to write back to the .properties file that has been compiled into your .jar, as far as I know, this is not possible; you can however, write that properties file to an external source.

File f = new File("my.properties");
OutputStream out = new FileOutputStream( f );
props.store(f, "a comment");

This will create the file alongside your .jar file.

If this is the route you choose, you might want to first try loading from the external file, and default to the internal one for defaults if external is not found.

Upvotes: 1

Related Questions