如何在不删除旧值的情况下写入属性文件
how to write in properties file without deleting old values
我想在不删除文件中先前写入的值的情况下写入属性文件。
例如,属性文件中有值
token = tokengenerated
现在当我再次设置新值时
token1 = tokensnew
然后属性文件应该显示
token = tokengenerated
token1 = tokensnew
将 true 作为第二个参数传递给 FileWriter 以打开 "append" 模式。
fout = new FileWriter("filename.txt", true);
您必须读取文件 (var1),然后将您的内容添加到 var1,然后将 var1 写入文件。
您应该读取文件并通过属性和流更新它。
下面是对您有帮助的代码片段。
public class ReadAndWriteProperties {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
String propertiesFileName = "config.properties";
File f = new File(propertiesFileName);
InputStream input = new FileInputStream(f);
if (input != null) {
props.load(input);
props.setProperty("token2", "tokensnew");
OutputStream out = new FileOutputStream(f);
props.store(out, "save");
}
}
}
我想在不删除文件中先前写入的值的情况下写入属性文件。 例如,属性文件中有值
token = tokengenerated
现在当我再次设置新值时
token1 = tokensnew
然后属性文件应该显示
token = tokengenerated
token1 = tokensnew
将 true 作为第二个参数传递给 FileWriter 以打开 "append" 模式。
fout = new FileWriter("filename.txt", true);
您必须读取文件 (var1),然后将您的内容添加到 var1,然后将 var1 写入文件。
您应该读取文件并通过属性和流更新它。
下面是对您有帮助的代码片段。
public class ReadAndWriteProperties {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
String propertiesFileName = "config.properties";
File f = new File(propertiesFileName);
InputStream input = new FileInputStream(f);
if (input != null) {
props.load(input);
props.setProperty("token2", "tokensnew");
OutputStream out = new FileOutputStream(f);
props.store(out, "save");
}
}
}