java讀取properties配置文件總結

java讀取properties配置文件總結

在平常項目開發和學習中,咱們難免會常常用到.propeties配置文件,例如數據庫c3p0鏈接池的配置等。而咱們常常讀取配置文件的方法有如下兩種:java

(1).使用getResourceAsStream()方法讀取配置文件。mysql

(2).使用InputStream()流去讀取配置文件。sql

注意在使用getResourceAsStream()讀取配置文件時,要特別注意配置文件的路徑的寫法。數據庫

this.getClass.getResourceAsStream(fileName)..使用類和資源文件在同一目錄下;工具

this.getclass.getLoader.getResourceAsStream(fileName)..資源文件和classes同級。學習

下面,我以一個實例來具體講述分別使用上述兩種方法來讀取.properties配置文件。測試

1.項目結構

 

在項目中,.properties文件的存放路徑基本有以上4種,全部在下面的測試中我會用2種方法讀取上面的4種不一樣路徑下的.properties文件。(對應上面的4個配置文件)this

(1).src下面config.properties包內的配置文件;url

1 name=\u5F20\u4E09
2 age=23

(2).和使用配置文件位於同一包裏面的.properties配置文件;spa

1 name=\u674E\u56DB
2 age=14

 

(3).src根目錄下面的配置文件;

1 jdbc.driver=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3306/goods
3 jdbc.usename=root
4 jdbc.password=123456

 

(4).位於另一個source文件夾裏面的配置文件。

1 dbuser=user
2 dbpassword=root
3 database=javaTest

 

 

2.java讀取properyies配置文件

首先,建立讀取.properties的工具類PropertiesUtil.java 

代碼清單【1】

  1 package read.propertiesFile.test;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.FileInputStream;
  5 import java.io.FileNotFoundException;
  6 import java.io.IOException;
  7 import java.io.InputStream;
  8 import java.text.MessageFormat;
  9 import java.util.Properties;
 10 
 11 
 12 /**
 13  * java讀取properties配置文件工具類
 14  * @author Administrator
 15  * 
 16  * 2016/12/04
 17  */
 18 public class PropertiesUtil 
 19 {
 20     public static final PropertiesUtil INSTANCE = new PropertiesUtil();
 21     
 22     public static PropertiesUtil getInstance()
 23     {
 24         return INSTANCE;
 25     }
 26     /**
 27      * 1.使用getSourceAsStream讀取配置文件
 28      */
 29     public void readPropertiesByGetSourceAsStream()
 30     {
 31         /*
 32          * 1.src下面config.properties包內的配置文件
 33          * test1.properties
 34          */
 35         InputStream is1 = JavaReadPropertiesTest.class.getClassLoader().getResourceAsStream("config/properties/test1.properties");
 36         
 37         /*
 38          * 2.讀取和JavaReadPropertiesTest類位於同一個包裏面的配置文件
 39          * test2.properties
 40          */
 41         InputStream is2 = JavaReadPropertiesTest.class.getResourceAsStream("test2.properties");
 42         
 43         /*
 44          * 3.讀取根目錄下面的配置文件
 45          * jdbc.properties
 46          */
 47         InputStream is3 = JavaReadPropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
 48         
 49         /*
 50          * 4.讀取位於另一個source文件夾裏面的配置文件
 51          * config.properties
 52          */
 53         InputStream is4 = JavaReadPropertiesTest.class.getClassLoader().getResourceAsStream("config.properties");
 54         Properties p = new Properties();
 55         System.out.println("使用getSourceAsStream()讀取配置文件...");
 56         try 
 57         {
 58             System.out.println("---讀取is1開始---");
 59             p.load(is1);
 60             System.out.println("test1.properties:name = "+ p.getProperty("name")
 61                     + ",age = " + p.getProperty("age"));
 62             System.out.println("---讀取is1結束---");
 63             System.out.println("-----------------------------------------------");
 64             
 65             System.out.println("---讀取is2開始---");
 66             p.load(is2);
 67             System.out.println("test2.properties:name = "+ p.getProperty("name")
 68                     + ",age = " + p.getProperty("age"));
 69             System.out.println("---讀取is2結束---");
 70             System.out.println("-----------------------------------------------");
 71             
 72             System.out.println("---讀取is3開始---");
 73             p.load(is3);
 74             System.out.println("jdbc.properties:");
 75             // 這裏的%s是java String佔位符
 76             System.out.println(String.format("jdbc.driver=%s",p.getProperty("jdbc.driver")));
 77             System.out.println(String.format("jdbc.url=%s",p.getProperty("jdbc.url")));
 78             System.out.println(String.format("jdbc.usename=%s",p.getProperty("jdbc.usename")));
 79             System.out.println(String.format("jdbc.password=%s",p.getProperty("jdbc.password")));
 80             System.out.println("---讀取is3結束---");
 81             System.out.println("-----------------------------------------------");
 82             
 83             System.out.println("---讀取is4開始---");
 84             p.load(is4);
 85             System.out.println(MessageFormat.format("dbuser={0}", p.getProperty("dbuser")));
 86             System.out.println(MessageFormat.format("dbpassword={0}", p.getProperty("dbpassword")));
 87             System.out.println(MessageFormat.format("database={0}", p.getProperty("database")));
 88             System.out.println("---讀取is4結束---");
 89         } 
 90         catch (IOException e) 
 91         {
 92             e.printStackTrace();
 93         }
 94         finally
 95         {
 96             if(null != is1)
 97             {
 98                 try {
 99                     is1.close();
100                 } 
101                 catch (IOException e) 
102                 {
103                     e.printStackTrace();
104                 }
105             }
106             
107             if(null != is2)
108             {
109                 try {
110                     is2.close();
111                 } 
112                 catch (IOException e) 
113                 {
114                     e.printStackTrace();
115                 }
116             }
117             
118             if(null != is3)
119             {
120                 try {
121                     is3.close();
122                 } 
123                 catch (IOException e) 
124                 {
125                     e.printStackTrace();
126                 }
127             }
128             
129             if(null != is4)
130             {
131                 try {
132                     is4.close();
133                 } 
134                 catch (IOException e) 
135                 {
136                     e.printStackTrace();
137                 }
138             }
139         }
140     }
141 
142     /**
143      * 2.使用InputStream讀取配置文件
144      */
145     public void readPropertiesByInputStream()
146     {
147         InputStream is1 = null;
148         InputStream is2 = null;
149         InputStream is3 = null;
150         InputStream is4 = null;
151         System.out.println("使用InputStream讀取配置文件...");
152             try 
153             {
154                 /*
155                  * 1.src下面config.properties包內的配置文件
156                  * test1.properties
157                  */
158                 is1 = new BufferedInputStream(new FileInputStream("src/config/properties/test1.properties"));
159                 
160                 /*
161                  * 2.讀取和JavaReadPropertiesTest類位於同一個包裏面的配置文件
162                  * test2.properties
163                  */
164                 is2 = new BufferedInputStream(new FileInputStream("src/read/propertiesFile/test/test2.properties"));
165                 
166                 /*
167                  * 3.讀取根目錄下面的配置文件
168                  * jdbc.properties
169                  */
170                 is3 = new BufferedInputStream(new FileInputStream("src/jdbc.properties"));
171                 
172                 /*
173                  * 4.讀取位於另一個source文件夾裏面的配置文件
174                  * config.properties
175                  */
176                 is4 = new BufferedInputStream(new FileInputStream("config/config.properties"));
177                 
178                 Properties p = new Properties();
179                 
180                 System.out.println("---讀取is1開始---");
181                 p.load(is1);
182                 System.out.println("test1.properties:name = "+ p.getProperty("name")
183                         + ",age = " + p.getProperty("age"));
184                 System.out.println("---讀取is1結束---");
185                 System.out.println("-----------------------------------------------");
186                 
187                 System.out.println("---讀取is2開始---");
188                 p.load(is2);
189                 System.out.println("test2.properties:name = "+ p.getProperty("name")
190                         + ",age = " + p.getProperty("age"));
191                 System.out.println("---讀取is2結束---");
192                 System.out.println("-----------------------------------------------");
193                 
194                 System.out.println("---讀取is3開始---");
195                 p.load(is3);
196                 System.out.println("jdbc.properties:");
197                 System.out.println(String.format("jdbc.driver=%s",p.getProperty("jdbc.driver")));
198                 System.out.println(String.format("jdbc.url=%s",p.getProperty("jdbc.url")));
199                 System.out.println(String.format("jdbc.usename=%s",p.getProperty("jdbc.usename")));
200                 System.out.println(String.format("jdbc.password=%s",p.getProperty("jdbc.password")));
201                 System.out.println("---讀取is3結束---");
202                 System.out.println("-----------------------------------------------");
203                 
204                 System.out.println("---讀取is4開始---");
205                 p.load(is4);
206                 System.out.println("config.properties:");
207                 System.out.println(MessageFormat.format("bduser={0}", p.getProperty("bduser")));
208                 System.out.println(MessageFormat.format("bdpassword={0}", p.getProperty("bdpassword")));
209                 System.out.println(MessageFormat.format("database={0}", p.getProperty("database")));
210                 System.out.println("---讀取is4結束---");
211                 
212             } 
213             catch (FileNotFoundException e) 
214             {
215                 e.printStackTrace();
216             }
217             catch (IOException e) 
218             {
219                 e.printStackTrace();
220             }
221             finally
222             {
223                 if(null != is1)
224                 {
225                     try {
226                         is1.close();
227                     } 
228                     catch (IOException e) 
229                     {
230                         e.printStackTrace();
231                     }
232                 }
233                 
234                 if(null != is2)
235                 {
236                     try {
237                         is2.close();
238                     } 
239                     catch (IOException e) 
240                     {
241                         e.printStackTrace();
242                     }
243                 }
244                 
245                 if(null != is3)
246                 {
247                     try {
248                         is3.close();
249                     } 
250                     catch (IOException e) 
251                     {
252                         e.printStackTrace();
253                     }
254                 }
255                 
256                 if(null != is4)
257                 {
258                     try {
259                         is4.close();
260                     } 
261                     catch (IOException e) 
262                     {
263                         e.printStackTrace();
264                     }
265                 }
266             }
267     }
268     }
269     

 

而後,JUnit測試 JavaReadPropertiesTest.java

代碼清單【2】

 1 package read.propertiesFile.test;
 2 
 3 import org.junit.Test;
 4 
 5 /**
 6  * java讀取properties配置文件
 7  * @author Administrator
 8  *
 9  */
10 
11 public class JavaReadPropertiesTest
12 {
13     /**
14      * 1.使用getSourceAsStream讀取配置文件測試
15      */
16     @Test
17     public void readPropertiesByGetSourceAsStreamTest()
18     {
19         PropertiesUtil.getInstance().readPropertiesByGetSourceAsStream();
20     }
21     
22     
23     /**
24      *  2.使用InputStream讀取配置文件測試
25      */
26     @Test
27     public void readPropertiesByInputStreamTest()
28     {
29         PropertiesUtil.getInstance().readPropertiesByInputStream();
30     }
31     
32     
33 
34 
35 }

 

3.輸出結果

 1 使用getSourceAsStream()讀取配置文件...
 2 ---讀取is1開始---
 3 test1.properties:name = 張三,age = 23
 4 ---讀取is1結束---
 5 -----------------------------------------------
 6 ---讀取is2開始---
 7 test2.properties:name = 李四,age = 14
 8 ---讀取is2結束---
 9 -----------------------------------------------
10 ---讀取is3開始---
11 jdbc.properties:
12 jdbc.driver=com.mysql.jdbc.Driver
13 jdbc.url=jdbc:mysql://localhost:3306/goods
14 jdbc.usename=root
15 jdbc.password=123456
16 ---讀取is3結束---
17 -----------------------------------------------
18 ---讀取is4開始---
19 dbuser=user
20 dbpassword=root
21 database=javaTest
22 ---讀取is4結束---
23 使用InputStream讀取配置文件...
24 ---讀取is1開始---
25 test1.properties:name = 張三,age = 23
26 ---讀取is1結束---
27 -----------------------------------------------
28 ---讀取is2開始---
29 test2.properties:name = 李四,age = 14
30 ---讀取is2結束---
31 -----------------------------------------------
32 ---讀取is3開始---
33 jdbc.properties:
34 jdbc.driver=com.mysql.jdbc.Driver
35 jdbc.url=jdbc:mysql://localhost:3306/goods
36 jdbc.usename=root
37 jdbc.password=123456
38 ---讀取is3結束---
39 -----------------------------------------------
40 ---讀取is4開始---
41 config.properties:
42 bduser=null
43 bdpassword=null
44 database=javaTest
45 ---讀取is4結束---

 

4.補充

在這裏,我在簡要介紹一下,讀取配置文件的所有配置信息的方法,以jdbc.properties文件爲例。

 1 /**
 2      * 3.循環讀取配置文件的所有信息
 3      */
 4     public void readAllInfo()
 5     {
 6         /*
 7          * 3.讀取根目錄下面的配置文件
 8          * jdbc.properties
 9          */
10         InputStream is3 = JavaReadPropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
11         Properties p = new Properties();
12         System.out.println("---讀取is3開始---");
13         try 
14         {
15             p.load(is3);
16             //讀取資源文件的全部的key值
17             Enumeration en= p.propertyNames();
18             System.out.println("讀取is3開始...");
19             while(en.hasMoreElements())
20             {
21                 String key = (String) en.nextElement();
22                 System.out.println(key + "=" + p.getProperty(key));
23                 
24             }
25             System.out.println("讀取is3結束...");
26         } 
27         catch (IOException e)
28         {
29             e.printStackTrace();
30             System.out.println("讀取資源文件出錯");
31         }
32         finally
33         {
34             if(null != is3)
35             {
36                 try 
37                 {
38                     is3.close();
39                     System.out.println("關閉is3...");
40                 }
41                 catch (IOException e) 
42                 {
43                     e.printStackTrace();
44                 }
45             }
46         }
47         
48     }

 

 輸出結果是:

1 ---讀取is3開始---
2 讀取is3開始...
3 jdbc.url=jdbc:mysql://localhost:3306/goods
4 jdbc.driver=com.mysql.jdbc.Driver
5 jdbc.usename=root
6 jdbc.password=123456
7 讀取is3結束...
8 關閉is3...

 

至此,讀取配置文件的總結就先這樣,後期有新的體會時,在進行增長。謝謝觀閱。

相關文章
相關標籤/搜索