Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove comments in properties file java

Tags:

java

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);
like image 556
Lalchand Avatar asked May 19 '11 12:05

Lalchand


People also ask

How do I uncomment a properties file?

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.

How do you uncomment multiple lines in properties file?

We can use Ctrl + / to comment multiple lines in properties file.

How remove value from properties file in java?

Properties properties = new Properties(); properties. load(reader); Then you can use the remove() method.

How do I comment in application properties file?

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.

How do you add comments to a property file in Java?

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.

Does Java properties supports inline comments?

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.

How to read and write to properties file in Java?

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.


3 Answers

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()));
like image 189
Nathan Ryan Avatar answered Oct 27 '22 22:10

Nathan Ryan


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 :) )

like image 36
DaveH Avatar answered Oct 27 '22 21:10

DaveH


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"));
like image 39
Christian Avatar answered Oct 27 '22 21:10

Christian