简体   繁体   中英

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.

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

FileWriter usage reference

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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