這是我原來的postgresql.conf \ t字符:java.util.Properties增加了對.store()
#
#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
我在運行時從Java中使用下面的代碼更新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");
怎麼回事錯在這裏:
我也用指定的編碼試過嗎?
_postgresql.conf_替換不是Java屬性文件。只有行[以#開頭(http://docs.oracle.com/javase/8/docs/api/java/util/Properties.html#load-java.io.Reader-)被視爲Java屬性中的註釋文件。 – McDowell
那麼?如何讀取/寫入.conf文件? 我使用了Properties,因爲它包含key = value格式。 參考: http://stackoverflow.com/questions/16273174/how-to-read-a-configuration-file-in-java –