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

Source: Internet
Author: User

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

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.