I have a properties file that i create manualy.
I can to get the propert in the file by getProperty() function, but I can't change it!
I try with setProperty() function, but the file isn't changed.
can u help me?
thanks!
zipi
You need to write the properties file again using store() (of which there are two variants). The setProperty() method changes the value of property stored in memory, not the value of the property in the file the properties were loaded from.
For further reading see the Properties Tutorial.
SetProperty() will only set the property during the runtime. It will not go and override your file property. It wont modify your file.
Did you try to call something like
prop.store(new FileOutputStream("config.properties"), null);after calling prop.setProperty method? Because it's the way you flush changes to file.
Without calling store changes are visible only in application memory.
Related
As part of my test base class, I have something like this:
seleniumJupiter.getConfig().setDefaultBrowser(BROWSER.getStringValue());
seleniumJupiter.getConfig().setScreenshotAtTheEndOfTests("whenfailure");
SeleniumJupiter.getConfig().takeScreenshotAsBase64AndPng();
and potentially 10-20 more config parameters. Could I somehow overwrite the whole selenium-jupiter.properties file and change some of the properties and other left default?
You can maintain your own copy of selenium-jupiter.properties in your project classpath, changing the values you need, and leaving the default values for the others. Then, you have two options to configure Selenium-Jupiter to use that properties:
Using a JVM property: -Dsel.jup.properties=/my-sel-jup.properties
Using an environmental variable: SEL_JUP_PROPERTIES=/my-sel-jup.properties
I have a DB connection using separated properties.
serverName=MyServerName
databaseName=MyDbName
instance=MyInstance
I want to override these with env.properties and this is working however the 'instance' property needs to be left blank. I've tried passing in the env.properties with a blank entry for 'instance' however it does not override the default 'MyInstance' (others are overridden fine).
Is this possible? If so, please may you give an example?
I have a resource bundle which consist of a few files, lets say:
address_en_us.properties
address_nl_nl.properties
address_fr_ca.properties
How do I obtain and merge two of the properties file with en_us being the 'default' property file?
Some background:
When I use one of the localized property, say, fr_ca, for each keys that are not localized in fr_ca, I want to use the default value specified in en_us
Just rename your "address_en_us.properties" to "address.properties" and this file is always used as 'default' property file.
One way I have seen work is to have the default values set in code, where it is hard-coded in java class. That way any property not set in properties file but used in code will always get the default value.
And from an architecture perspective this is also a really good idea, shipping products with default properties values in code, overridden by the values in properties file. And if you make it so that the product (Android app in my perspective) makes a server call at start-up to check and overwrite the values of properties files then you can add a lot of flexibility to the product.
I used the Eclipse string externalisation wizard to extract strings which are then stored in a configuration file.
Although when I run it I keep getting an NPE whenever a resource or a System.getProperty() is called.
I can't seem to find documentation on this, is there a reason for this?
Many thanks.
You won't find your externalized through System.getProperty().
From the docs of System.getProperty():
Gets the system property indicated by the specified key.
When I run the String externalization wizzard I get a class called Messages on which I can call a .getString method like this:
Messages.getString("Test.STR0")
I am using the java properties file construct. At the beginning I read it to populate a dialog, but I also give the users the ability to change the values in the dialog and click save. This causes me to call the setProperty method of the properties file. Now, since this webapp can exist over multiple browsers, all changing the same file, I want to be able to "lock" the properties file whenever I am in the "save" method. How can I accomplish this? I've seen similar questions refering to FileLock, but I am unsure if this applies to the properties file construct. Is each "setProperty" a different write?
Thanks!
setProperty merely updates the set of properties, it does not write the file. You have to call the store(OutputStream out, String header) method to actually write out the file to disk, and that is the time where you'll have to "lock" the file.
You can use FileLock to prevent multiple users from writing to the file at the same time, however, each time your user wants to save, you will need to reload the property file from disk, change the property, and write the file back again, all without releasing the file lock, to make sure no stale data is saved.