0
使用下面的代碼我可以存儲在屬性文件中,但問題是存儲最後存儲的值。我的意思是,如果一個價值val1
,val2
,val3
... 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
感謝
我沒有得到你......你可以在這裏更新我的代碼,以便我能理解嗎? – user3411418
添加此行table.load(new FileInputStream(file));在創建名爲'table'的屬性的行下面。 – rsudha
這是不工作:( – user3411418