简体   繁体   English

如何在属性文件中写入而不删除旧值

[英]how to write in properties file without deleting old values

I want to write in properties file without removing earlier written values in file. 我想在属性文件中写入而不删除文件中较早的写入值。 for Eg there is value in properties file 例如,属性文件中有值

token = tokengenerated

Now when I again set new value like 现在,当我再次设置新值时

token1 = tokensnew

Then the properties file should show 然后属性文件应显示

token = tokengenerated
token1 = tokensnew 

You should read file and update it through properties and streams. 您应该读取文件并通过属性和流对其进行更新。

below is the code snippet is help you. 下面的代码段可以帮助您。

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");
        }

    }

}

Pass true as a second argument to FileWriter to turn on "append" mode. 将true作为第二个参数传递给FileWriter以打开“附加”模式。

fout = new FileWriter("filename.txt", true); fout = new FileWriter(“ filename.txt”,true);

FileWriter usage reference FileWriter使用参考

您必须先读取文件(var1),然后将内容添加到var1,然后将var1写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM