I have to edit the existing file named root.propertis and update it without saving in to another file
Following is the sample proprety file.
root.label.getmore=Mehr Apps
root.msg.apps=Apps
root.label.2.2=Coupons
root.label.35.2=MSNBC
root.label.43.2=PBS Kids
root.label.47.2=Yahoo! Digest
I have to update the string in the file "root.label.43.2=PBS Kids" to "root.label.43.2=Updated"
But i need to save in the same file (root.propertis )by repalcing the string root.label.43.2=PBS Kids.No need to update the changes in another properties file.
Use java.util.Properties:
File f = new File("root.properties");
FileInputStream fis = new FileInputStream(f);
Properties p = new Properties();
p.load(fis);
fis.close();
p.setProperty("root.label.43.2", "Updated");
The call p.store() to save to a file.
Note exception handling has been omitted.
You could use the following sequence to change Properties load the properties with load(), setProperty(key,value) and finally call store() to write it back.
Reading and writing a .properties file can easily be achieved by using the Properties class (see javadoc).
So you can
Read the file into a Properties instance using the Properties#load method
Update the Properties instance using the Properties#setProperty method
Write the Properties to file using the Properties#store method
Related
I am working on some automation stuff, in which I just want to write the values into the application.properties file as shown below:
project.ids = 01234, 56789, 14587,...
So here, my key(project.ids) will be the same, I just want to append the ids to the previously stored values (shown above). Whenever my service will be called a new project id will be generated so I just want to append or store the value in the property file to the same key(project.ids).
Could anyone help or suggest here how can I achieve this?
Any suggestions around how to write the value to the application.properties file in Java?
I would suggest to use the Environment class of Spring. But it is only used to read properties and not write them.
what you could do is follow this way:
Load the .properties file:
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("application.properties");
prop.load(in);
Get the value as it is of the property
String ids = prop.getProperty("project.ids");
Set the new value
String ids = prop.setProperty("project.ids", ids + ", 12324...");
PS: I wouldn't play around with the .properties file much, it is mostly used for configurations. Try saving your ids in a DB, Spring can be very helpful for that.
I have a file under resources folder src/test/resources/file.xml
and another under src/test/resources/test.properties. I want to set a property in properties file to point to file.xml. How can I achieve this?
Say I have a property
test.file = file.xml
and my java class reads the file as follows:
File cert = new File(fileName); // fileName is the value of test.file
This does not work however.
You can use Properties class to read and write to config files.
Firstly, you need to find the relative path for the resources using
below steps
Secondly, you can configure and load the test properties file
Finally, read the property value and append with the resource
directory
Code:
String rootDirectory=System.getProperty("user.dir");
String resourceDirectory=rootDirectory+"src/test/resources/";
//Configure property File
Properties properties = new Properties();
properties.load(new FileInputStream(resourceDirectory+"test.properties"));
PropertyConfigurator.configure(properties);
//To get the property value
String tempFileName=properties.getProperty("test.file");
//filename Needs to be changed as below
File cert = new File(resourceDirectory+tempFileName);
I'm having some trouble when reading from a .config file in a Java application.
I have a module, let's call it Configuration_Reader that implements the next interface:
Interface
int getDelayValue();
int getRepValue();
...
For example, the getDelayValue() implementation is as follows:
Implementation
InputStream fis = new FileInputStream(
new File("").getAbsolutePath()+"/config/config.properties");
props.load(fis);
fis.close();
PropertyConfigurator.configure(props);
...
public int getDelayValue() {
return getIntProperty("delayValue");
}
There's no error nor exception when reading the value, but my issue is that if I update the value on the config file and call the getDelayValue() function again, it always returns the original value the property had when I launched the application. So, it is not updating (I assume it is not reading the file again) even if debugging I can see that the function is being called appropriately.
Any tips? Thanks in advance
You need to reload the config file to see the changes, i.e. this code
InputStream fis = new FileInputStream(
new File("").getAbsolutePath()+"/config/config.properties");
props.load(fis);
fis.close();
PropertyConfigurator.configure(props);
needs to run again, after the config has changed.
If you're using Java 7 you can use a WatchService to detect if the file has changed.
Another alternative would be to reload the config file every time a config value is accessed, but I wouldn't recommend this if the value is accessed frequently or concurrently from multiple threads.
I have a properties file that is in my classpath. My requirement is to change some properties in this file based on some inputs I get from arguments. The nature of arguments decide whether I need to change the properties and if yes which properties to change. The problem is that all classpath entries are loaded at the application startup time, so changing from within my application would not have any effect. How do I overcome this problem?
One possible solution I can think of is to not add this properties file in classpath but add after modifications are done. Is it viable? What can be a good solution?
It doesn't matter whether this file is on your classpath or not. It is a file: if you overwrite its contents, it will have changed. There isn't some in-memory copy that magically gets made at startup. This is very different from classes that are loaded in and which might need change at runtime.
Properties files that adhere to the right format can be read into a java.util.Properties object. You could do that, use the object to alter the properties as needed, then write it back out to the file. Check the store and load methods in that class. Mind that if you use the versions that take an Output/InputStream, the encoding is hard-coded. If the file's encoding is anything else than ISO-8859-1, use a method with an appropriate Writer/Reader.
Depends on how your application is deployed. If your properties files is inside a jar, you won't be able to directly change that properties file since its packaged and zipped up in an archive. You can instead as someone else mentioned load those properties into an object, and then store/write out to an external location, probably a URL based location. URL is convenient because it gets you access to virtually any location and it has that nifty openStream() method for loading properties. Your application could then look for the new file on load, and default to the application startup version if it fails to read/load from the new location.
Here is a sample code:
Properties p = new Properties();
File f = new File("file");
InputStream in = new FileInputStream(f);
p.load(in);
p.put("key", "blah");
OutputStream out = new FileOutputStream(f);
// If no comments p.store(writer);
p.store(out, "properties");
You need to first remove that property from the property file and then re-define it. Their is no way to directly modify the properties file.
Below is an example:
Properties pproperties = new Properties();
if (properties.containsKey("key1")) {
properties.remove("key1");
properties.setProperty("key1", "value1");
properties.store(new FileOutputStream("file.properties"), null);
}
I am writing a java web application that reads properties from a .properties file. Since I do not know the absolute path of the .properties file, because it depends on the environment the application will run on in the future, I have to load it with "getClass().getResourceAsStream":
Properties props = new Properties();
props.load(getClass().getResourceAsStream("test.properties"));
message = props.getProperty("testdata");
This works as expected. Now I want to change the value for testdata in the file. But I cannot open an Outputstream to write to, because I still don't know the path of the .properties file.
props.setProperty("testdata", "foooo");
props.store(new FileOutputStream("?????"), null);
Is there a way to get the path of the file or can I use the established Properties-object somehow? Any ideas are welcome that allow me to change the .properties file.
You can get an URL by using getResource() rather than using getResourceAsStream()
You can then use that URL to read from and write to your properties file.
File myProps = new File(myUrl.toURI());
FileInputStream in = new FileInputStream(myProps);
Etc.
The Properties class includes a store method that can be used to save the properties back to the stream that was read in getClass().getResourceAsStream("test.properties").