when i updated the properties file the comments are also updated along the data .is there any possible way to remove the comments or updating the data without comment.
Here i update the file 4 times each time the date-time stamp append as a comment
#Thu May 19 17:53:42 GMT+05:30 2011
Key_1=DSA_1024
#Thu May 19 17:53:43 GMT+05:30 2011
Key_2=DSA_1024
#Thu May 19 17:53:43 GMT+05:30 2011
Key_3=DSA_1024
#Thu May 19 17:53:44 GMT+05:30 2011
Key_4=DSA_1024
code
Properties prop=new Properties();
String currentDirectary=System.getProperty("user.dir");
String path=currentDirectary+"/Resource/Key.Properties";
FileOutputStream out=new FileOutputStream(path,true);
prop.setProperty("keyName","DSA_1024");
prop.store(out, null);
The Short Answer You can “uncomment a line” in a configuration file by removing the # at the start of the line. Or, to “comment out” a line, add a # character to the start of the line.
We can use Ctrl + / to comment multiple lines in properties file.
Properties properties = new Properties(); properties. load(reader); Then you can use the remove() method.
In addition to properties and blank lines, the application. properties field may contain comments. To comment a line, just put the hash character at the beginning of a line.
comments always start in a new line with space followed by # or ! character and followed by comments text. java properties files support single-line comments only. One is using the Semicolon symbol ; placed at beginning of each line followed by comment text.
Java properties support single-line comments which start with a newline followed by # symbol. Any text that starts with # symbol \ is ignored by the java compiler when reading the properties files. Does java properties supports inline comments.
Normally, Java properties file is used to store project configuration data or settings. In this tutorial, we will show you how to read and write to/from a .properties file. A simple Maven project structure for testing. 1. Write to the properties file Set the property key and value, and save it somewhere. The path/to/config.properties is created. 2.
I once had to do this because a consumer of the properties file couldn't handle the properties. The comment produced by the store
method is well defined, so it's easy enough to skip over it:
Writer stringOut = new StringWriter();
properties.store(stringOut, null);
String string = stringOut.toString();
String sep = System.getProperty("line.separator");
out.write(string.substring(string.indexOf(sep) + sep.length()));
From the JavaDocs for properties.store()
If the comments argument is not null, then an ASCII # character, the comments string, and a line separator are first written to the output stream. Thus, the comments can serve as an identifying comment.
Next, a comment line is always written, consisting of an ASCII # character, the current date and time (as if produced by the toString method of Date for the current time), and a line separator as generated by the Writer.
The only option I can think of would be to write your ownOutputStream implementation to drop the comments. ( Or just learn to live with them :) )
Here's an improvement of the hack above which uses the right encoding as well.
FileOutputStream out = new FileOutputStream(filename);
ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
props.store(arrayOut, null);
String string = new String(arrayOut.toByteArray(), "8859_1");
String sep = System.getProperty("line.separator");
String content = string.substring(string.indexOf(sep) + sep.length());
out.write(content.getBytes("8859_1"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With