.properties 文件被覆盖。如何克服呢?
The .properties file is getting overridden. How to overcome from that?
java 中的 config.properties 文件在每次执行后都会被覆盖。我想要的是,以前的数据也应该和新更新的一起保存。
下面是我用来将值存储到 config.properties 文件的代码。我的 config.properties 文件已经包含了一些数据。
try (OutputStream output = new FileOutputStream("path\to\config.properties")) {
Properties prop = new Properties();
// set the properties value
prop.setProperty("IFCode", "34253");
// save properties to project root folder
prop.store(output, null);
System.out.println(prop);
} catch (IOException io) {
io.printStackTrace();
}
这里是我的 config.properties 文件数据,它已经存在于那里:-
#Tue Jul 23 02:38:34 EDT 2019
File=987
Read=098
现在,当我 运行 上面的代码时,这些已经可用的数据被省略了,新的数据是这样写的:-
#Tue Jul 23 02:38:34 EDT 2019
IFCode=34253
我想要的基本上是:-
#Tue Jul 23 02:38:34 EDT 2019
File=987
Read=098
IFCode=34253
如何使用 java 解决这个问题?
尝试为您的 FileOutputStream 添加布尔附加参数:
OutputStream output = new FileOutputStream("path\to\config.properties", true)
FileOutputStream(File file, boolean append)
Creates a file output stream to write to the file represented by the specified File object. Check for reference:
https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
解决方法如下:
try (OutputStream output = new FileOutputStream("path\to\config.properties",true)) {
Properties prop = new Properties();
// set the properties value
prop.setProperty("IFCode", "34253");
// save properties to project root folder
prop.store(output, null);
System.out.println(prop);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
java 中的 config.properties 文件在每次执行后都会被覆盖。我想要的是,以前的数据也应该和新更新的一起保存。
下面是我用来将值存储到 config.properties 文件的代码。我的 config.properties 文件已经包含了一些数据。
try (OutputStream output = new FileOutputStream("path\to\config.properties")) {
Properties prop = new Properties();
// set the properties value
prop.setProperty("IFCode", "34253");
// save properties to project root folder
prop.store(output, null);
System.out.println(prop);
} catch (IOException io) {
io.printStackTrace();
}
这里是我的 config.properties 文件数据,它已经存在于那里:-
#Tue Jul 23 02:38:34 EDT 2019
File=987
Read=098
现在,当我 运行 上面的代码时,这些已经可用的数据被省略了,新的数据是这样写的:-
#Tue Jul 23 02:38:34 EDT 2019
IFCode=34253
我想要的基本上是:-
#Tue Jul 23 02:38:34 EDT 2019
File=987
Read=098
IFCode=34253
如何使用 java 解决这个问题?
尝试为您的 FileOutputStream 添加布尔附加参数:
OutputStream output = new FileOutputStream("path\to\config.properties", true)
FileOutputStream(File file, boolean append) Creates a file output stream to write to the file represented by the specified File object. Check for reference: https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
解决方法如下:
try (OutputStream output = new FileOutputStream("path\to\config.properties",true)) {
Properties prop = new Properties();
// set the properties value
prop.setProperty("IFCode", "34253");
// save properties to project root folder
prop.store(output, null);
System.out.println(prop);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}