[Java] configuration file concept, Java operations on the configuration file

Source: Internet
Author: User

[Java] configuration file concept, Java operations on the configuration file

*. Ini, *. properties, and *. xml are all configuration files. These configuration files are often regarded as gods. Since the day we were in touch with computers, they were warned not to confuse them. In fact, they have the same structure as the HashMap of java. They are all containers that store key-value pairs, but the configuration files are key-value pairs of attributes and attribute values, if HashMap doesn't know what it is, refer to the Collection class in [Java] Java-Upgraded data structure in Java I wrote earlier (click to open the link ), but why can't we touch it? Because these key-valueyi pairs generally record the program running parameters, and many of them are initialization parameters. Generally, these parameters are run from the beginning of the program, that is, when the program is born, the program is closed, that is, the values required for program death. Just as the name, gender, and Place of Birth attributes of each of us, we will continue to use them from birth to death.

The configuration file has three extension names *. ini ,*. ini has been seen in many win32 programs. It is also labeled as a "system file" and deleted or something will be warned by Windows. In fact, it is just a configuration file, after reading this article, you will feel that the configuration file is the same. In Linux or in some Java projects, the configuration file is *. properties, the most common is the popular international SSH file, which is the starting point for international configuration files.

In fact, *. ini and *. properties are exactly the same. Just like *. dll and *. jar, they are all necessary runtime libraries of win32 or java programs. *. The dll may not be familiar to anyone who has not written large win32 programs, *. jar, you must know that many java programs must be written in *. jar package makes development more convenient, and your java project must carry these *. jar package can run. You can also observe that most win32 programs exist *. dll in the directory, because these *. dll packages are used during compilation! Similar to java Plug-ins.

There is also a new type of configuration file. xml, which is familiar to everyone. If you are not familiar with the web. xml configuration file of the Jsp project, do not start the project. Why *. xml is a new configuration file, and *. xml is the product of the new era? That's because *. ini and *. properties do not support Chinese, while *. xml supports Chinese.

The configuration file operations in Java are as simple as operating a HashMap. The configuration file class Properties is also in java. util. * The package also involves file read/write operations, so java is also required. io. * for readers who do not know about Java input and output, refer to [Java] input and output and the new string StringBuilder after jdk1.5. (click to open the link ). See the following procedure:

 

Import java. io. *; import java. util. *; public class propertiesTest {public static void main (String [] args) throws IOException {// initialize the Properties class first // After initialization, A properties object, Properties = new properties (), that stores the key-value Pair appears in the memory (); // set three attributes later. This can be analogous to the put Method of HashMap. It is also explained by setting a Chinese value for the key-value pair *. ini ,*. properties and *. xml supports the properties of Chinese characters. setProperty ("username", "aaa"); properties. setProperty ("chinese", "chinese"); properties. setProperty ("password", "123"); // at this time, the properties object in the memory has three key-value pairs // use the store method and storeToXML method to save the properties of the properties object record in the memory to the configuration file on the disk. // The method only supports files. output stream object, file of a single File object does not work // The second parameter is the comment properties of this configuration File. store (new FileOutputStream ("c: \. ini ")," Chinese "); properties. store (new FileOutputStream ("c: \. properties ")," comment "); properties. storeToXML (new FileOutputStream ("c: \. xml ")," comment "); // clear the properties object properties = new Properties (); // run c: \. the properties stored in ini are read in properties. load (new FileInputStream ("c: \. ini "); // change the password attribute value to 456properties. put ("password", "456"); // print the properties Object System in the memory. out. println (properties); // you can obtain the username attribute value System in the properties object in the memory. out. println (properties. getProperty ("username"); // clear the properties object and read c: \. read the attributes stored in the xml configuration file *. the xml method is different properties = new Properties (); properties. loadFromXML (new FileInputStream ("c: \. xml "); // print the attribute System in the memory. out. println (properties );}}

After the program runs, the following items are output in the console:

 

On disk C, three configuration files a. ini, au. properties, and a. xml are obtained,

The contents of a. ini and a. properties are the same except for comments. The content of a. ini is as follows,

We can see that. ini is an old configuration file that does not support Chinese characters at all. The store method of Properties class in Java can only transcode and save Chinese characters for you. Note that the load method does not read comments, after you read these attributes back to the program using load, they are displayed normally without transcoding. Therefore, when struts is internationalized, the Properties class can be used for transcoding and output, and no tools are required. Java has corresponding methods. Configuration files, as programs, generally store important information and do not need to be shown to users. Therefore, attribute values are generally only numbers.

The content of a. xml is rich, as follows:

 

<? Xml version = "1.0" encoding = "UTF-8" standalone = "no"?> <! DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment> comment </comment> <entry key = "password"> 123 </entry> <entry key = "chinese"> chinese </entry> <entry key = "username"> aaa </entry> </properties>
However, the loadFromXML method of the Properties object only reads the key-value Pair and compares the running results,

 

You can see that all data is read by key-value pairs of attributes and attribute values.

By explaining the concept of the configuration file and Java's operations on the configuration file, it is estimated that you have basically mastered *. ini ,*. properties ,*. what are the xml configuration files? You can also see the basic structure of these configuration files.

Related Article

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.