F:\\demo.properties File Contents:
#\u65b0\u589e\u4fe1\u606f
#Wed Sep 11:16:24 CST 2016
province= Guangdong
Tt= near the egg
city= Foshan City
Java code:
public static void Test () throws IOException {
FileWriter writer = new FileWriter ("F:\\demo.properties");
FileReader reader = new FileReader ("F:\\demo.properties");
Properties P = new properties ();
P.load (reader);
SYSTEM.OUT.PRINTLN (P);
P.setproperty ("DD", "Zhong Jian Pu");
P.setproperty ("CC", "Dog Baby");
P.setproperty ("BB", "dog Left");
P.setproperty ("AA", "Iron egg");
P.store (writer, "new information");
SYSTEM.OUT.PRINTLN (P);
Reader.close ();
Writer.close ();
}
Console Output :
{}
{dd=, aa= iron eggs, bb= dog left, cc= dog Baby}
the problems that arise:
One, the read content is empty,
Second, the contents of the previous properties file are not overwritten
The reason is:
FileWriter writer = new FileWriter ("F:\\demo.properties");
FileWriter Open the file by default is overwrite, that is, once the above sentence is executed, then the contents of the original file is emptied
So you emptied the file when you didn't have p.load (in), loaded the properties.
Modified as follows:
public static void Test () throws IOException {
FileReader reader = new FileReader ("F:\\demo.properties");
Properties P = new properties ();
P.load (reader);
SYSTEM.OUT.PRINTLN (P);
P.setproperty ("DD", "Zhong Jian Pu");
P.setproperty ("CC", "Dog Baby");
P.setproperty ("BB", "dog Left");
P.setproperty ("AA", "Iron egg");
FileWriter writer = new FileWriter ("F:\\demo.properties");
P.store (writer, "new information");
SYSTEM.OUT.PRINTLN (P);
Reader.close ();
Writer.close ();
}
Console output:
{province= Guangdong, tt= near Egg, city= Foshan}
{dd=, province= Guangdong, tt= near eggs, aa= iron eggs, bb= dog left, city= Foshan, cc= dog Baby}
Problem solving:
because the FileWriter open the file by default is overwrite,
Is that once the above sentence is executed, the contents of the original file are emptied
So you have not p.load (in), loading properties of the time to empty the file so be sure to pay attention to the opportunity to open FileWriter, to grasp the FileWriter writer = new FileWriter ( "F:\\demo.properties"); Code Location