Use Properties to read the configuration file and obtain the specific content value. properties configuration file
Sometimes, when writing a configuration file, you need to know whether the read content is correct. We need to test it to see if the read content is the same as what we want. A tool class is written here to read the content in the configuration file.
1. Create a java project, import the required jar package, and create a configuration file, such:
2. configuration file content:
1 driver=com.mysql.jdbc.Driver2 url=jdbc:mysql://localhost:3306/csdn3 user=root4 pwd=1234565 initsize=16 maxactive=17 maxwait=50008 maxidle=19 minidle=1
3. Read the configuration file tool class:
1 package com. cnblogs. daliu_it; 2 3 import java. io. fileInputStream; 4 import java. util. properties; 5 6/** 7 * read configuration file 8 * @ author daliu_it 9 */10 public class GetProperties {11 public static void getProperties () {12 try {13 // java. util. properties14/* 15 * Properties class is used to read the properties file. This class can be used to read the content in the configuration file in a format similar to Map 16*17 * The content format in the properties file is similar: user = openlab, then the left side of the equal sign is the key, and the right side of the equal sign is value18 */19 Properties pro. P = new Properties (); 20 21/* 22 * use Properties to read the configuration file 23 */24 FileInputStream Fi = new FileInputStream ("config. properties "); 25/* 26 * after the file is read through Properties, the stream remains open and should be closed by ourselves. 27 */28 prop. load (fisms); 29 FIPS. close (); 30 System. out. println ("successfully loaded configuration file"); 31 32/* 33 * after loading is complete, the content (key) on the left of the equal sign in the text file can be used) to obtain the content (value) on the right of the equal sign. 34 * Properties can be regarded as a Map35 */36 String driver = prop in disguise. getProperty ("driver "). trim (); 37 String url = prop. getProperty ("url "). trim (); 38 String user = prop. getProperty ("user "). trim (); 39 String pwd = prop. getProperty ("pwd "). trim (); 40 System. out. println ("driver:" + driver); 41 System. out. println ("url:" + url); 42 System. out. println ("user:" + user); 43 System. out. println ("pwd:" + pwd); 44 45} catch (Exception e) {46 e. printStackTrace (); 47} 48} 49}
4. Test class:
1 package com. daliu_it.test; 2 3 import java. SQL. SQLException; 4 5 import org. junit. test; 6 7 import com. cnblogs. daliu_it.GetProperties; 8 9 public class testCase {10 11/** 12 * Get the configuration file 13 * @ throws SQLException14 */15 @ Test16 public void testgetProperties () throws SQLException {17 GetProperties poperties = new GetProperties (); 18 poperties. getProperties (); 19} 20}
5 .:
Author: daliu_it
Blog Source: http://www.cnblogs.com/liuhongfeng/p/4176224.html
The copyright of this article is shared by the author and the blog, but the above statement must be retained and placed on the article page without the author's consent. Thank you for your cooperation.