java.util.Properties在.store()上添加\ t字符

时间:2014-07-23 11:02:54

标签: java postgresql properties properties-file

这是我原来的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



我在运行时使用以下代码更新postgresql.conf:

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();


此代码导致以下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


不知怎的,' \ t'字符被添加或被视为转义序列。

我也尝试过使用以下命令编码:

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


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

这里出了什么问题?

1 个答案:

答案 0 :(得分:2)

没有错。如果您在java.util.Poperties的源代码中看到所有标签都替换为\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();
    }