Java knowledge accumulation-examples of using the properties class

Source: Internet
Author: User

Recently, I have been studying the properties configuration file and suddenly encountered a Java class named properties. This class is inherited from hashtable and provides a method similar to the implementation class hashmap of map. At that time, I was inspired by my attention and research on this category. By looking for information and debugging, I found that this category could help us at the beginning of the program running. By parsing the pre-file (including some parameters required by the Program), you can obtain the configuration information required for running the program and store it in the properties class for the program to call.

The role of the properties class is the same as its name. It mainly solves the attribute problem. To put it bluntly, the relationship between the properties class and the configuration file is very ambiguous. The common use cases of the properties class are summarized as follows:

1. read data from the properties configuration file, parse the key-value pair, and store the data in the properties class.

2. read data from xml configuration price, parse key-value pairs, and store them in the properties class.

3. output the key-Value Pair stored in the properties class to the properties configuration file.

4. output the key-Value Pair stored in the properties class to the xml configuration file.

The four functions correspond to these four situations:

1 // read the configuration information of the properties file to 2 Public void readpropfile () {3 properties prop = new properties (); 4 enumeration <string> en = NULL; 5 string key = NULL; 6 7 inputstream input = NULL; 8 try {9 input = This. getclass (). getclassloader (). getresourceasstream ("test. properties "); 10 prop. load (input); 11 en = (enumeration <string>) prop. propertynames (); 12 // prop. list (system. out); 13 while (en. hasmoreelements () {14 key = en. nextelement (); 15 system. out. println (Key + "---" + Prop. getproperty (key); 16} 17} catch (exception e) {18 E. printstacktrace (); 19} finally {20 try {21 input. close (); 22} catch (exception E2) {23 e2.printstacktrace (); 24} 25} 26} 27 28 // read the configuration information of the XML file to 29 public void readxmlfile () {30 properties prop = new properties () in the properties class (); 31 enumeration <string> en = NULL; 32 string key = NULL; 33 34 inputstream input = NULL; 35 try {36 input = This. getclass (). getclassloader (). getresourceasstream ("test. XML "); 37 prop. loadfromxml (input); 38 en = (enumeration <string>) prop. propertynames (); 39 // prop. list (system. out); 40 while (en. hasmoreelements () {41 key = en. nextelement (); 42 system. out. println (Key + "---" + Prop. getproperty (key); 43} 44} catch (exception e) {45 e. printstacktrace (); 46} finally {47 try {48 input. close (); 49} catch (exception E2) {50 e2.printstacktrace (); 51} 52} 53} 54 55 // set the key-value pair in the properties class, 56 public void printprop () {57 properties prop = new properties (); 58 prop. setproperty ("content1", "there is no problem"); 59 prop. setproperty ("content2", "Hello World"); 60 61 // printwriter outputfile = NULL; 62 fileoutputstream outputfile = NULL; 63 try {64 // outputfile = new printwriter (new file ("src/target. properties "); 65 outputfile = new fileoutputstream (" src/target. properties "); 66 prop. store (outputfile, "test target file"); 67} catch (exception e) {68 E. printstacktrace (); 69} finally {70 try {71 outputfile. close (); 72} catch (exception E2) {73 e2.printstacktrace (); 74} 75} 76} 77 78 // set the key-value pair in the properties class, save to XML file 79 public void printxml () {80 properties prop = new properties (); 81 prop. setproperty ("content1", "there is no problem"); 82 prop. setproperty ("content2", "Hello World"); 83 84 // printwriter outputfile = NULL; 85 fileoutputstream outputfile = NULL; 86 try {87 // outputfile = new printwriter (new file ("src/target. properties "); 88 outputfile = new fileoutputstream (" src/target. XML "); 89 prop. storetoxml (outputfile, "test target XML file"); 90} catch (exception e) {91 E. printstacktrace (); 92} finally {93 try {94 outputfile. close (); 95} catch (exception E2) {96 e2.printstacktrace (); 97} 98} 99}

You can use the list method of the properties class or the store Method for output to a file.

The output content is as follows:

Properties File

#test target file#Thu Mar 14 15:59:01 CST 2013content2=hello worldcontent1=there is no problem

XML file

1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>2 <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">3 <properties>4 <comment>test target xml file</comment>5 <entry key="content2">hello world</entry>6 <entry key="content1">there is no problem</entry>7 </properties>

It should be emphasized that reading XML files only supports the format shown in the above Code, which is specified in properties. DTD. XML configurations of other rules cannot be correctly identified and an exception is thrown. There are not so many requirements for the properties file.

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.