Summary of the Properties class in dark horse programmer _ java

Source: Internet
Author: User

Objective:

1. Understand the properties file, understand its meaning, and create the properties file correctly.

2. the java. util. Properties class is used to operate the properties file.

1. Understand the properties File

1. The properties file is a text file.

2. There are two types of properties File Syntax: annotation and attribute configuration.

Note: Add # To the front

Attribute configuration: Write the configuration information of an attribute in the form of "Key = value.

3. A property configuration value in the properties file can wrap, but the key cannot wrap. The value line feed is represented.

4. spaces before and after properties configuration key values are ignored during parsing.

5. The properties file can have only keys but no values. You can also set only the key and equal sign instead of the value. However, no key is allowed for an attribute configuration.

For example, the following properties file:

 

Ii. Explanation of java. util. Properties class

There is a class Properties under the java. util package, which is mainly used to read the configuration files (files ending with. properties and xml files) of the project ). The Properties class is a subclass of Hashtable. That is to say, it has the characteristics of Map sets. The Properties constructor has two functions, one with no parameters, and the other with a Properties object as the parameter.

1. hierarchical structure of the Properties class

Java. lang. Object

Java. util. Dictionary <K, V>

Java. util. Hashtable <Object, Object>

Java. util. Properties

From the perspective of hierarchy, the Properties class implements the Map interface. Because HashTable implements the Map interface, the Properties class is essentially a simple Map container.

In fact, the Properties class itself represents operations on a Map structure. The properties file itself represents a set of "key-value pairs. Therefore, the Properties class belongs to the collection container family. Before use, you should create a Properties container. In fact, it is to create a Properties object without parameters by default. In the future, add a "key-Value Pair" to it in another way ".

2. Relationship between properties files and Properties classes

You can use the properties file to fill in the Properties class.

You can also fill in the Properties class through the xml file.

You can load the Properties File Information in absolute paths or relative paths.

Iii. Practice

1. Load the properties File Information in absolute mode.

2. persists the Properties object to a properties file or an xml file.

3. Modify and persist the properties file.

Test properties file:

 

Test class:

1/** 2 * Properties class test 3 * User: Zhi Shengyong 4 */5 public class Test4 {6 7 public static void main (String args []) throws IOException {8 9 System. out. println (""); 10 11 Properties prop = new Properties (); 12 prop. put ("name", "Zhi Shengyong"); 13 prop. put ("hobbies", "programming and Tourism"); 14 prop. put ("Hometown", "Guizhou Bijie"); 15 16 createProperFile ("E: \ tt. properties ", prop); 17 18 readProperFile (" E: \ tt. properties "); 19 20 Map <String, String> map = New HashMap <String, String> (); 21 map. put ("gender", "male"); 22 map. put ("education", "Undergraduate"); 23 modifyProperFile ("E: \ tt. properties ", map); 24} 25 26 27/*** 28 * Create a properties file and write a key-value pair to it, including properties and xml format 29 * @ param fileName 30 * @ param keyValues 31 */32 public static void createProperFile (String fileName, Properties prop) {33 System. out. println (""); 34 // create properties File 35 36 File properFile = new File (fileName. s Ubstring (0, fileName. lastIndexOf (".") + ". properties"); 37 if (! ProperFile. exists () {38 try {39 properFile. createNewFile (); 40} catch (IOException e) {41 // TODO Auto-generated catch block 42 System. err. print ("An error occurred while creating the file! "); 43 return; 44} 45} 46 47 try {48 49 FileWriter fw = new FileWriter (properFile); 50 prop. store (fw, "I wrote in through properties"); 51 OutputStream fw1 = new FileOutputStream (fileName. substring (0, fileName. lastIndexOf (". ") + ". xml "); 52 prop. storeToXML (fw1, "I wrote in through properties"); 53 fw1.close (); 54 fw. close (); 55 56 System. out. println (""); 57} catch (IOException e) {58 // TODO Auto-generated catch block 59 System. err. print ("failed to write data to the properties file! "); 60 return; 61} 62} 63 64 65/*** 66 * read properties file 67 * @ param properFileName 68 * @ return 69 * @ throws IOException 70 */71 public static Properties readProperFile (String properFileName) throws IOException {72 73 File file = new File (properFileName); 74 Properties prop = null; 75 if (! File. exists () {76 77 System. out. print ("the file does not exist! "); 78 return null; 79} 80 81 try {82 System. out. println (""); 83 FileReader fr = new FileReader (file); 84 // create a Properties container 85 prop = new Properties (); 86 // load the properties file information 87 prop from the stream. load (fr); 88 // cyclically output configuration information 89 for (Object key: prop. keySet () {90 System. out. println (key + "=" + prop. get (key); 91} 92 93 94} catch (FileNotFoundException e) {95 // TODO Auto-generated catch block 96 System. out. p Rint ("the file does not exist! "); 97} 98 99 return prop; 100} 101 102 103 104/*** 105 * modify the properties file, and persistently save 106 * @ param properFileName107 * @ param map108 * @ throws IOException109 */110 Public static void modifyProperFile (String properFileName, Map <String, String> map) throws IOException {111 112 Properties prop = new Properties (); 113 114 if (properFileName. endsWith (". xml ") | properFileName. endsWith (". XML ") {115 116 prop. loadFromXML (new FileInputStream (properFileName); 117 for (String key: map. keySet () {118 prop. put (key, map. get (key); 119} 120 OutputStream OS = new FileOutputStream (properFileName); 121 prop. storeToXML (OS, "I have been repaired"); 122 OS. close (); 123} else {124 prop. load (new FileReader (properFileName); 125 for (String key: map. keySet () {126 prop. put (key, map. get (key); 127} 128 FileWriter fw = new FileWriter (properFileName); 129 prop. store (fw, "I have been modified"); 130 fw. close (); 131} 132} 133}

Running result diagram:

 

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.