Java Foundation - Properties Collection of IO Stream Objects

                Java Foundation - Properties Collection of IO Stream Objects

                                  Author: Yin Zhengjie

Copyright statement: original works, please do not reprint! Otherwise held liable.

 

 

 

1. Characteristics of the Properties collection

  The Properties class represents a persistent set of properties. Properties can be stored in or loaded from a stream, and each key in the property list and its corresponding value is a string. Summarizing its obvious features are as follows:

    1>. A subclass of Hashtable, the methods in the map collection are available;

    2>. The set has no generic type, and the keys and values ​​are all strings;

    3>. It is a persistent attribute set, and the key value can be stored in the set or in persistent devices (hard disk, U disk, CD-ROM). The source of the key value can also be a persistent device.

    4>. There are methods combined with flow count such as: " ", " ", " ", " ".load(InputStream inStream)load(Reader reader)store(OutputStream out, String comments)store(Writer writer, String comments)

 

2. Common methods of Properties collection

1>.Properties collection stores key-value pairs

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note6;
 8 
 9 import java.util.Properties;
10 import java.util.Set;
11 
12 public class PropertiesDemo {
13     public static void main(String[] args) {
14         Properties p = new Properties();
15         //1>. Use the setProperty method to store key-value pairs. The bottom layer calls the put method of the parent class (Hashtable), which can be viewed through the source code. 
16          p.setProperty("Name", "yinzhengjie" );
 17          p.setProperty("Age", "18" );
 18          p.setProperty("Hobby", "Cosplay" );
 19          
20          System.out.println( p);
 21          // 2>. Get the value by key, and return null if the given key value does not exist. 
22          String value = p.getProperty("Name1" );
 23          System.out.println(value);
 24          
25          // 3>. Traverse the Properties collection through the stringPropertyNames method, which stores the keys in the collection to the Set collection, Similar to the method keySet of the Map interface. 
26          Set<String>27          for (String key : set) {
 28              System.out.println(key+"==="+ p.getProperty(key));
 29          }
 30          
31      }
 32  }
 33  
34  
35  /* 
36  The execution result of the above code is as follows:
 37  {Hobby=Cosplay, Age=18, Name=yinzhengjie}
 38  null
 39  Hobby===Cosplay
 40  Age===18
 41  Name===yinzhengjie
 42  */

2>.(load method) The stream object reads the key-value pair in the file and saves it to the collection

 

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note6;
 8 
 9 import java.io.FileReader;
10 import java.io.IOException;
11 import java.util.Properties;
12 
13 public class PropertiesDemo {
14     public static void main(String[] args) throws IOException {
15         Properties p = new Properties();
 16          // Create a character stream object to read the file 
17          FileReader fr = new FileReader("yinzhengjie.properties" );
 18          // Call the method load of the collection, pass the character input stream, put The contents of the file are loaded into the p object. 
19          p.load(fr);
 20          // After loading, the fr stream object has no use value, just turn it off. 
21          fr.close();
 22          System.out.println(p);
 23  
24      }
 25  }
 26  
27  /* 
28  The execution result of the above code is as follows:
 29  {name=yinzhengjie, age=18, [email protected] }
 30  */

3>. (store method) Write the key-value pairs in the collection to a file and save

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note6;
 8 
 9 import java.io.FileWriter;
10 import java.io.IOException;
11 import java.util.Properties;
12 import java.util.Set;
13 
14 public class PropertiesDemo {
15     public static void main(String[] args) throws IOException {
 16          Properties p = new Properties();
 17          // 1>. Use the setProperty method to store key-value pairs, and the bottom layer calls the put method of the parent class (Hashtable), which can be viewed through the source code. 
18          p.setProperty("Name", "yinzhengjie" );
 19          p.setProperty("Age", "18" );
 20          p.setProperty("Hobby", "Cosplay" );
 21          
22          FileWriter fw = new FileWriter( "yinzhengjie.properties" );
 23          // Write the key-value pair to the file, and use the collection method store to pass the character output stream. 
24          p.store(fw, "Add by yinzhengjie");    
25         
26     }
27 }

  After execution, a new file will be generated with the following content:

 

3. Small test knife

  Limit the number of game trials, define a file with the content: "times=5", this parameter indicates the number of times the player can experience the game, when the value is 0, the utility ends.

1 #Sat May 05 17:38:45 GMT+08:00 2018
2 times=5
yinzhengjie.properties file content
1  /* 
2  @author :yinzhengjie
 3  Blog: http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 
4  EMAIL:[email protected]
 5  */ 
6  
7  package cn.org.yinzhengjie.note6;
 8  
9  import java.io.FileInputStream;
 10  import java.io.FileOutputStream;
 11  import java.util.Properties;
 12  
13  /* 
14  * Properties do not belong to the io stream system, java Under the .util package.
 15  * Use as a subclass of Hashtable
 16  * 
 17   */ 
18  public class PropertiesDemo {
19 
20     public static void main(String[] args) throws Exception {
21         Properties p = new Properties();
22         p.load(new FileInputStream("yinzhengjie.properties"));
23         
24         String times = p.getProperty("times");
25         int i = Integer.parseInt(times);
26         
27         if(i <= 0){
28             System.out.println("试用结束");
29         }else{
 30              System.out.println("Currently: " + (5 - i + 1) + " times, remaining: " + (i - 1) + " times" );
 31              play();
 32              p .setProperty("times", i - 1 + "" );
 33              p.store( new FileOutputStream("yinzhengjie.properties"), null );
 34          }
 35      }
 36  
37      private  static  void play() {
 38          System.out .println("Playing game..." );
 39      }
 40  
41 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325167811&siteId=291194637