Java programs cannot overwrite the contents of a previous properties file when using the store method with the properties class __javase

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.