F:\\demo.properties File Contents:
#\u65b0\u589e\u4fe1\u606f
#Wed Sep 11:16:24 CST 2016
province= Guangdong
tt= near 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 lian");
P.setproperty ("CC", "Dog Doll");
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=, cc=, Dog Doll}
Problems that arise:
One, the read content is empty,
Second, the contents of the previous properties file are not overwritten
The reasons are:
FileWriter writer = new FileWriter ("F:\\demo.properties");
FileWriter Open the file by default is overwritten, that is, once the above sentence is executed, then the contents of the original file is emptied
So you're not p.load (in), and the file is emptied when the properties are loaded.
The following changes are followed:
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 lian");
P.setproperty ("CC", "Dog Doll");
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 egg, aa= iron egg, bb= dog left, city= Foshan, cc= Dog Doll}
Problem solving:
is because the way FileWriter opens the file is overwritten by default,
Is that once the above sentence is executed, the contents of the original file are emptied.
So you're not p.load (in), and the file is emptied when the properties are loaded.
So be sure to pay attention to open FileWriter timing, to grasp FileWriter writer = new FileWriter ("F:\\demo.properties"); Code location
Java programs cannot overwrite the contents of a previous properties file when using the store method with the Properties class