Basically, I have to overwrite a certain property in a .properties file through a Java app, but when I use Properties.setProperty() and Properties.Store() it overwrites the
The Properties
API doesn't provide any methods for adding/replacing/removing a property in the properties file. The model that the API supports is to load all of the properties from a file, make changes to the in-memory Properties
object, and then store all of the properties to a file (the same one or a different one).
But the Properties
API is not unusual in the respect. In reality, in-place updating of a text file is difficult to implement without rewriting the entire file. This difficulty is a direct consequence of the way that files / file systems are implemented by a modern operating system.
If you really need to do incremental updates, then you need to use some kind of database to hold the properties, not a ".properties" file.
Other Answers have suggested the following approach in various guises:
Properties
object.Properties
object.Properties
object on top of existing file.This works for some use-cases. However the load / save is liable to reorder the properties, remove embedded comments and white space. These things may matter1.
The other point is that this involves rewriting the entire properties file, which the OP is explicitly trying to avoid.
1 - If the API is used as the designers intended, property order, embedded comments, and so on wouldn't matter. But lets assume that the OP is doing this for "pragmatic reasons".
public class PropertiesXMLExample {
public static void main(String[] args) throws IOException {
// get properties object
Properties props = new Properties();
// get path of the file that you want
String filepath = System.getProperty("user.home")
+ System.getProperty("file.separator") +"email-configuration.xml";
// get file object
File file = new File(filepath);
// check whether the file exists
if (file.exists()) {
// get inpustream of the file
InputStream is = new FileInputStream(filepath);
// load the xml file into properties format
props.loadFromXML(is);
// store all the property keys in a set
Set<String> names = props.stringPropertyNames();
// iterate over all the property names
for (Iterator<String> i = names.iterator(); i.hasNext();) {
// store each propertyname that you get
String propname = i.next();
// set all the properties (since these properties are not automatically stored when you update the file). All these properties will be rewritten. You also set some new value for the property names that you read
props.setProperty(propname, props.getProperty(propname));
}
// add some new properties to the props object
props.setProperty("email.support", "donot-spam-me@nospam.com");
props.setProperty("email.support_2", "donot-spam-me@nospam.com");
// get outputstream object to for storing the properties into the same xml file that you read
OutputStream os = new FileOutputStream(
System.getProperty("user.home")
+ "/email-configuration.xml");
// store the properties detail into a pre-defined XML file
props.storeToXML(os, "Support Email", "UTF-8");
// an earlier stored property
String email = props.getProperty("email.support_1");
System.out.println(email);
}
}
}
The output of the program would be:
support@stackoverflow.com
If you just want to override 1 prop why not just add parameter to your java command. Whatever you provide in your properties file they will be overrided with properties args.
java -Dyour.prop.to.be.overrided="value" -jar your.jar
I do the following method:-
Yes it is possible. If you don't want to delete your content from the property file. Just read and replace the string from the file.
String file="D:\\path of your file\abc.properties";
Path path = Paths.get(file);
Charset charset = StandardCharsets.UTF_8;
String content = new String(Files.readAllBytes(path), charset);
content = content.replaceAll("name=anything", "name=anything1");
Files.write(path, content.getBytes(charset));
The above code will not delete content from your file. It just replace the part of content from the file.
import java.io.*;
import java.util.*;
class WritePropertiesFile
{
public static void main(String[] args) {
try {
Properties p = new Properties();
p.setProperty("1", "one");
p.setProperty("2", "two");
p.setProperty("3", "three");
File file = new File("task.properties");
FileOutputStream fOut = new FileOutputStream(file);
p.store(fOut, "Favorite Things");
fOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}