0

this is my original postgresql.conf:

#
#Wed Jul 23 16:13:09 IST 2014
lc_monetary='English_India.1252'    # locale for monetary formatting
listen_addresses='*'        # what IP address(es) to listen on;
max_connections=100     # (change requires restart)
port=5433
shared_buffers=128MB        # min 128kB
log_timezone='Asia/Calcutta'
timezone='Asia/Calcutta'
datestyle='iso, dmy'
lc_numeric='English_India.1252'     # locale for number formatting
lc_time='English_India.1252'        # locale for time formatting
default_text_search_config='pg_catalog.english'
lc_messages='English_India.1252'        # locale for system error message



I'm updating postgresql.conf at runtime from java using following code:

Properties input = new java.util.Properties();
InputStream reader=new 
FileInputStream(System.getProperty("user.dir")+"/postgresql.conf");
input.load(reader);
input.setProperty("port", String.valueOf(Installer.newport));
OutputStream confFile=new FileOutputStream(System.getProperty("user.dir")+"\\postgresql.conf");
input.store(confFile, "");
confFile.close();


This code results in following postgresql.conf :

#
#Wed Jul 23 16:13:09 IST 2014
lc_monetary='English_India.1252'\t\t\t\# locale for monetary formatting
listen_addresses='*'\t\t\# what IP address(es) to listen on;
max_connections=100\t\t\t\# (change requires restart)
port=5433
shared_buffers=128MB\t\t\t\# min 128kB
log_timezone='Asia/Calcutta'
timezone='Asia/Calcutta'
datestyle='iso, dmy'
lc_numeric='English_India.1252'\t\t\t\# locale for number formatting
lc_time='English_India.1252'\t\t\t\t\# locale for time formatting
default_text_search_config='pg_catalog.english'
lc_messages='English_India.1252'\t\t\t\# locale for system error message


Somehow the '\t' characters are getting added or rather seen as escape sequence.

I've also tried by specifying encoding with:

Reader reader = new InputStreamReader(new FileInputStream(System.getProperty("user.dir")+"/postgresql.conf"),"UTF-8");


and

OutputStreamWriter confFile=new OutputStreamWriter(new FileOutputStream(System.getProperty("user.dir")+"\\postgresql.conf"),"UTF-8");


Whats going wrong here?

Ruturaj Patil
  • 608
  • 1
  • 10
  • 25
  • _postgresql.conf_ is not a Java properties file. Only lines [beginning with #](http://docs.oracle.com/javase/8/docs/api/java/util/Properties.html#load-java.io.Reader-) are considered comments in a Java properties file. – McDowell Jul 23 '14 at 11:51
  • So? How to read/write .conf files? I used Properties as it contains key=value format. Reference: http://stackoverflow.com/questions/16273174/how-to-read-a-configuration-file-in-java – Ruturaj Patil Jul 23 '14 at 12:25

1 Answers1

2

Nothing goes wrong. If you can see in the source code of java.util.Poperties all tabs are replaced with \t

 private String saveConvert(String theString,
                               boolean escapeSpace,
                               boolean escapeUnicode) {
        int len = theString.length();
        int bufLen = len * 2;
        if (bufLen < 0) {
            bufLen = Integer.MAX_VALUE;
        }
        StringBuffer outBuffer = new StringBuffer(bufLen);

        for(int x=0; x<len; x++) {
            char aChar = theString.charAt(x);
            // Handle common case first, selecting largest block that
            // avoids the specials below
            if ((aChar > 61) && (aChar < 127)) {
                if (aChar == '\\') {
                    outBuffer.append('\\'); outBuffer.append('\\');
                    continue;
                }
                outBuffer.append(aChar);
                continue;
            }
            switch(aChar) {
                case ' ':
                    if (x == 0 || escapeSpace)
                        outBuffer.append('\\');
                    outBuffer.append(' ');
                    break;
                case '\t':outBuffer.append('\\'); outBuffer.append('t');
                          break;
                case '\n':outBuffer.append('\\'); outBuffer.append('n');
                          break;
                case '\r':outBuffer.append('\\'); outBuffer.append('r');
                          break;
                case '\f':outBuffer.append('\\'); outBuffer.append('f');
                          break;
                case '=': // Fall through
                case ':': // Fall through
                case '#': // Fall through
                case '!':
                    outBuffer.append('\\'); outBuffer.append(aChar);
                    break;
                default:
                    if (((aChar < 0x0020) || (aChar > 0x007e)) & escapeUnicode ) {
                        outBuffer.append('\\');
                        outBuffer.append('u');
                        outBuffer.append(toHex((aChar >> 12) & 0xF));
                        outBuffer.append(toHex((aChar >>  8) & 0xF));
                        outBuffer.append(toHex((aChar >>  4) & 0xF));
                        outBuffer.append(toHex( aChar        & 0xF));
                    } else {
                        outBuffer.append(aChar);
                    }
            }
        }
        return outBuffer.toString();
    }
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Thanks for pointing out. For now I extended Properties class and changed the part where '\t' was being written. Can you suggest any better solution? – Ruturaj Patil Jul 23 '14 at 13:37
  • try to change the tabs to blanks I think it is a better solution. – Jens Jul 23 '14 at 13:42
  • I did that but it still adds '\' . I've successfully changed the code to my requirements, just waiting for any better fix. – Ruturaj Patil Jul 24 '14 at 05:36
  • @RuturajPatil Ok so the `#` will also be an special char for `java.util.Properties`. In this case the bestt sollution will be the extended implementation. – Jens Jul 24 '14 at 05:55