2

I want to write data in properties file using java.I am able to write the data in file by using java,but i am getting comments like date and time everytime by default.

Please anyone suggest how to avoid or remove comments in the proeprties file using java

Here is my code:

File file = new File("C:/Software/app.properties");
Properties properties = new Properties();
FileOutputStream fileOut = new FileOutputStream(file, true);
properties.setProperty(""+data.split(Constants.DATA_SPLIT)[1]+"", value);
properties.store(fileOut, null);
fileOut.close();

My o/p is O/put is:

#Wed Jun 04 13:23:54 IST 2014 CurrnerURL=J;SLDAJGLLASJGKPJ #Wed Jun 04 13:25:54 IST 2014 CurrnerURL=J;SLDAJGLLASJGKPJ

I dont want comment session in file. Please suggest me.

Jk1
  • 11,233
  • 9
  • 54
  • 64
user3016945
  • 265
  • 1
  • 6
  • 10
  • 1
    Show us some of your code, then we can point out what's wrong. – Ray Jun 04 '14 at 12:17
  • Are you using log4j or something to write to properties file? Post the code please – ngrashia Jun 04 '14 at 12:24
  • possible duplicate of [Properties.store() - suppress timestamp comment](http://stackoverflow.com/questions/6184335/properties-store-suppress-timestamp-comment) – Jk1 Jun 04 '14 at 15:14

1 Answers1

5

It is not possible by default.

If you need this you must Override the methode store0 of java.util.Properties Here the part of the original class.

private void store0(BufferedWriter bw, String comments, boolean escUnicode)
         throws IOException
     {
         if (comments != null) {
             writeComments(bw, comments);
         }
         bw.write("#" + new Date().toString());
         bw.newLine();
         synchronized (this) {
             for (Enumeration e = keys(); e.hasMoreElements();) {
                 String key = (String)e.nextElement();
                 String val = (String)get(key);
                 key = saveConvert(key, true, escUnicode);
                 /* No need to escape embedded and trailing spaces for value, hence
                  * pass false to flag.
                  */
                 val = saveConvert(val, false, escUnicode);
                 bw.write(key + "=" + val);
                 bw.newLine();
             }
         }
         bw.flush();
     }
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Using the Writer variant of the method using StringWriter and removing the first line before writing to the file (check for startsWith # if paranoid!), would be a relatively ok workaround if the file is not huge. – henry700 Jun 12 '21 at 14:49