gpt4 book ai didi

java - 为什么它不存储所有键值而是存储最后更新的值?

转载 作者:行者123 更新时间:2023-12-02 06:04:54 26 4
gpt4 key购买 nike

使用下面的代码我可以存储在属性文件中,但问题是它存储的是最后存储的值。我的意思是,如果存储值 val1val2val3...val5 它只存储 var5 值不是其他...有什么方法可以做到吗?

    prop = new Properties();

try {
/* //set the properties value
prop.setProperty(Key, value);

System.out.println("Updating the value for:"+Key);
//save properties to project testdata.properties file
prop.store(new FileOutputStream(System.getProperty("user.dir")
+ "xyz.properties"), null); */

file = new File(System.getProperty("user.dir")
+ "\\src\\test\\java\\config\\testdata123.properties");
Properties table = new Properties();

table.setProperty(Key, value);

System.out.println("Properties has been set in HashTable:" + table);

System.out.println("The Key values are :" + value);
//saving the properties in file
saveProperties(table);

System.out.println("After the change in HashTable:" + table);
//saving the properties in file


} catch (IOException ex) {
ex.printStackTrace();
return "Fail" + "While updating the testdata.properties file";
}

return "Pass";
}

public static void saveProperties(Properties p) throws IOException {
FileOutputStream fr = new FileOutputStream(file);
p.store(fr, "Properties");
System.out.println("After saving properties:" + p);
}

static void loadProperties(Properties p) throws IOException {
FileInputStream fi = new FileInputStream(file);
p.load(fi);
fi.close();

System.out.println("After Loading properties:" + p);
}

完整代码:

   public static String setPropertiesValue(String Key, String value) {

try {
file = new File(System.getProperty("user.dir")
+ "\\src\\test\\java\\config\\xyz.properties");
Properties table = new Properties();

table.load(new FileInputStream(file));

table.setProperty(Key, value);

System.out.println("Properties has been set in HashTable:" + table);

System.out.println("The Key values are :" + value);
//saving the properties in file
saveProperties(table);

System.out.println("After the change in HashTable:" + table);
//saving the properties in file


} catch (IOException ex) {
ex.printStackTrace();
return "Fail" + "While updating the testdata.properties file";
}

return "Pass";
}

public static void saveProperties(Properties p) throws IOException {
FileOutputStream fr = new FileOutputStream(file);
p.store(fr, "Properties");

System.out.println("After saving properties:" + p);
}

static void loadProperties(Properties p) throws IOException {
FileInputStream fi = new FileInputStream(file);
p.load(fi);
fi.close();

System.out.println("After Loading properties:" + p);
}
enter code here

谢谢

最佳答案

您正在创建一个名为“表”的新属性,并在每次将属性保存到文件中时覆盖您的文件。相反,从现有文件加载属性并设置新的键、值并保存。 希望这能起作用!

    import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class Test {
static File file = null;

/**
* @param args
*/
public static void main(String[] args) {
setPropertiesValue("name1", "value1");
setPropertiesValue("name2", "value2");
setPropertiesValue("name3", "value3");
setPropertiesValue("name4", "value4");
setPropertiesValue("name5", "value5");
}

public static String setPropertiesValue(String Key, String value) {

try {
file = new File(System.getProperty("user.dir") + "\\src\\test\\java\\config\\xyz.properties");

Properties table = new Properties();

table.load(new FileInputStream(file));

table.setProperty(Key, value);

System.out.println("Properties has been set in HashTable:" + table);

System.out.println("The Key values are :" + value);
// saving the properties in file
saveProperties(table);

System.out.println("After the change in HashTable:" + table);
// saving the properties in file

} catch (IOException ex) {
ex.printStackTrace();
return "Fail" + "While updating the testdata.properties file";
}

return "Pass";
}

public static void saveProperties(Properties p) throws IOException {
FileOutputStream fr = new FileOutputStream(file);
p.store(fr, "Properties");

System.out.println("After saving properties:" + p);
}

static void loadProperties(Properties p) throws IOException {
FileInputStream fi = new FileInputStream(file);
p.load(fi);
fi.close();

System.out.println("After Loading properties:" + p);
}
}

属性文件的结果(值)如下

属性

2014 年 3 月 13 日星期四 12:18:25 GMT

名称5=值5
名称4=值4
名称3=值3
名称2=值2
名称1=值1
这就是你想要得到的结果吗?或者我错过了什么吗?

关于java - 为什么它不存储所有键值而是存储最后更新的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22374703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com