Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between load () and store () in properties

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what is the difference between load () and store () in properties. The article is very detailed and has certain reference value. Interested friends must read it!

1.load

Load (InputStream instream) reads the list of attributes (key and element pairs) from the input stream

Load (Reader reader) reads a list of attributes (key and element pairs) from the input character stream in a simple line-oriented format.

The load method loads the input stream. Only after the input stream is loaded can you use some methods to read the properties file.

Please note here: "only", "only"

For example:

Public static void function4 () throws IOException {Properties p=new Properties (); FileReader fr=new FileReader ("f:\\ 110.properties"); String str=p.getProperty ("98K"); p.load (fr); fr.close (); System.out.println (str);}

The code does not report an error, but cannot read the key-value pair that exists in the file, but returns null

Positive solution: the input stream must be loaded before it can be read (4pm 5-line swap)

It is extremely difficult to find this error in long and large Socket programming.

2.store

Store (OutputStream out,String comments) writes the list of attributes (keys and element pairs) in this Properties table to the output stream in a format suitable for loading into the Properties table using the load (InputStream) method

Store (Writer writer,String comments) writes the list of attributes (keys and element pairs) from this Properties table to the output characters in a format suitable for using the load (Reader) method

The store method writes to the output stream, and only after setting the key-value pair of the properties object can it be written to the output stream to achieve real output to the file.

Please note here: "only", "can"

For example:

Public static void function3 () throws IOException {Properties p=new Properties (); FileWriter fw=new FileWriter ("f:\\ 110.properties"); p.store (fw, "describe here"); p.setProperty ("98k", "50D"); fw.close ();}

The code will not report an error, or even help you write the file timestamp, but the key-value pair is not written to the file.

Positive solution: key-value pairs must be stored before you can write to the output stream (4pm 5-line swap)

Always be clear that properties is essentially a collection. Don't interpret its load and store methods as "connecting" IO streams. It's a big mistake.

The above is all the content of the article "what is the difference between load () and store () in properties". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report