?
J2SE API 讀取 Properties 文件六種方法
http://webservices.ctocio.com.cn/115/8689615.shtml
?
1。使用 Java .util. Properties 類的load()方法
示例:InputStreamin=lnewBufferedInputStream(newFileInputStream(name));
Propertiesp=newProperties();
p.load(in);
2。使用 java .util.ResourceBundle類的getBundle()方法
示例:ResourceBundlerb=ResourceBundle.getBundle(name,Locale.getDefault());
3。使用 java .util.PropertyResourceBundle類的構造函數
示例:InputStreamin=newBufferedInputStream(newFileInputStream(name));
ResourceBundlerb=newPropertyResourceBundle(in);
4。使用class變量的getResourceAsStream()方法
示例:InputStreamin=JProperties.class.getResourceAsStream(name);
Propertiesp=newProperties();
p.load(in);
5。使用class.getClassLoader()所得到的 java .lang.ClassLoader的getResourceAsStream()方法
示例:InputStreamin=JProperties.class.getClassLoader().getResourceAsStream(name);
Propertiesp=newProperties();
p.load(in);
6。使用 java .lang.ClassLoader類的getSystemResourceAsStream()靜態方法
示例:InputStreamin=ClassLoader.getSystemResourceAsStream(name);
Propertiesp=newProperties();
p.load(in);
補充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStreamin=context.getResourceAsStream(path);
Propertiesp=newProperties();
p.load(in);
?
這個類的作用在于幫你解決連接數據庫時的 " 硬編碼 " 問題 , 即驅動類 , 連接字符串 , 用戶名 , 密碼這些信息可以通過資源文件來獲得 , 這種做法既增加了安全性 , 又使代碼容易維護 .
??
處理數據庫資源文件的類 ? DBConfig. java
?
這個類的作用在于幫你解決連接數據庫時的 " 硬編碼 " 問題 , 即驅動類 , 連接字符串 , 用戶名 , 密碼這些信息可以通過資源文件來獲得 , 這種做法既增加了安全性 , 又使代碼容易維護 .
??
處理數據庫資源文件的類 ? DBConfig. java
?
- import ? java .util. Properties ;??
- import ? java .io.*;??
- ??
- public ? class ?DBConfig?{??
- ???? private ? static ?Object?initLock?=? new ?Object();??
- ??
- ???? private ? static ?DBConfig?dbconfig?=? null ;??
- ??
- ???? private ? Properties ?props?=? null ;??
- ??
- ???? public ? static ?DBConfig?getInstance()?{??
- ???????? if ?(dbconfig?==? null )?{??
- ???????????? synchronized ?(initLock)?{??
- ???????????????? if ?(dbconfig?==? null )?{??
- ????????????????????dbconfig?=? new ?DBConfig();??
- ????????????????}??
- ????????????}??
- ????????}??
- ???????? return ?dbconfig;??
- ????}??
- ??
- ???? private ? synchronized ? void ?loadProperties()?{??
- ????????props?=? new ? Properties ();??
- ???????? try ?{??
- ????????????System.out.println( "Load?pro?file" );??
- ????????????InputStream?in?=?getClass().getResourceAsStream( "/db. properties " );??
- ????????????props.load(in);??
- ????????}? catch ?(Exception?e)?{??
- ????????????e.printStackTrace();??
- ????????}??
- ????}??
- ??
- ???? public ?String?getProperty(String?propName)?{??
- ???????? if ?(props?==? null )?{??
- ????????????loadProperties();??
- ????????}??
- ???????? return ?props.getProperty(propName);??
- ????}??
- }??
- import ? java .util. Properties ; ??
- import ? java .io.*; ??
- ??
- public ? class ?DBConfig?{ ??
- ???? private ? static ?Object?initLock?=? new ?Object(); ??
- ??
- ???? private ? static ?DBConfig?dbconfig?=? null ; ??
- ??
- ???? private ? Properties ?props?=? null ; ??
- ??
- ???? public ? static ?DBConfig?getInstance()?{ ??
- ???????? if ?(dbconfig?==? null )?{ ??
- ???????????? synchronized ?(initLock)?{ ??
- ???????????????? if ?(dbconfig?==? null )?{ ??
- ????????????????????dbconfig?=? new ?DBConfig(); ??
- ????????????????} ??
- ????????????} ??
- ????????} ??
- ???????? return ?dbconfig; ??
- ????} ??
- ??
- ???? private ? synchronized ? void ?loadProperties()?{ ??
- ????????props?=? new ? Properties (); ??
- ???????? try ?{ ??
- ????????????System.out.println( "Load?pro?file" ); ??
- ????????????InputStream?in?=?getClass().getResourceAsStream( "/db. properties " ); ??
- ????????????props.load(in); ??
- ????????}? catch ?(Exception?e)?{ ??
- ????????????e.printStackTrace(); ??
- ????????} ??
- ????} ??
- ??
- ???? public ?String?getProperty(String?propName)?{ ??
- ???????? if ?(props?==? null )?{ ??
- ????????????loadProperties(); ??
- ????????} ??
- ???????? return ?props.getProperty(propName); ??
- ????} ??
- }??
import java .util. Properties ; import java .io.*; public class DBConfig { private static Object initLock = new Object(); private static DBConfig dbconfig = null; private Properties props = null; public static DBConfig getInstance() { if (dbconfig == null) { synchronized (initLock) { if (dbconfig == null) { dbconfig = new DBConfig(); } } } return dbconfig; } private synchronized void loadProperties() { props = new Properties (); try { System.out.println("Load pro file"); InputStream in = getClass().getResourceAsStream("/db. properties "); props.load(in); } catch (Exception e) { e.printStackTrace(); } } public String getProperty(String propName) { if (props == null) { loadProperties(); } return props.getProperty(propName); } }
?
?
資源文件 ?db. properties
url=jdbc:mysql://localhost:3306/example driver=org.gjt.mm.mysql.Driver username=root password=123654 |
? 在連接數據庫的代碼中 , 可以通過以下方式得到驅動類 ,url,username,password
?
- String?driver???????=???DBConfig.getInstance().getProperty( "driver" );??
- String?url??????????=???DBConfig.getInstance().getProperty( "url" );??
- String?username?=???DBConfig.getInstance().getProperty( "username" );??
- String?password?=???DBConfig.getInstance().getProperty( "password" );??
- String?driver???????=???DBConfig.getInstance().getProperty( "driver" ); ??
- String?url??????????=???DBConfig.getInstance().getProperty( "url" ); ??
- String?username?=???DBConfig.getInstance().getProperty( "username" ); ??
- String?password?=???DBConfig.getInstance().getProperty( "password" );??
String driver = DBConfig.getInstance().getProperty("driver"); String url = DBConfig.getInstance().getProperty("url"); String username = DBConfig.getInstance().getProperty("username"); String password = DBConfig.getInstance().getProperty("password");
?
?
P.S.
請注意這三個文件的位置 , 建議放在同一個目錄下
?
一.
Properties
簡介?
java
.util.
Properties
繼承自
java
.util.Hashtable?
Properties
類表示一個持久的屬性集.
Properties
可保存在流中或從流中加載.屬性列表中每個鍵及其對應值都是一個字符串.?
二.基本方法?
2.1 如何從輸入流中加載屬性文件?
使用load(InputStream is)方法:?
- Properties ? properties ?=? new ? Properties ();??
- InputStream?is?=? new ?FileInputStream( "conn. properties " );??
- properties .load(is);??
- is.close();??
2.2 如何讀屬性文件中的值?
使用getProperties(String key)方法:
- String?temp?=? properties .getProperties(String?key);??
<注>重載的方法getProperties(String key, String default)方法 將在查詢不到值的情況下,返回default.?
即: 如果 null ==
properties
.getProperties(String key);?
則有 default ==
properties
.getProperties(String key, String default);?
2.3 如何獲取屬性文件中的所有的鍵?
使用propertyNames()方法,該方法返回是鍵的枚舉.
- Enumeration?enumeration?=? properties .propertyNames();??
2.4 如何修改屬性文件中的值?
使用
- setProperties(String?key,?String?value)??
方法.?
<注>該方法調用的 Hashtable 的put方法.如果鍵存在,則修改值;如果鍵不存在,則添加值.?
2.5 如何存儲屬性文件到輸出流?
使用
store
(OutputStream os, String description)方法:
- Properties ? properties ?=? new ? Properties ();??
- OutputStream?os?=? new ?FileOutputStream( "test. properties " );??
- String?description?=? " store ? properties ?to?test. properties " ;??
- properties . store (os,?description);??
- os.close();??
2.6 如何清空所有值?
使用
- clear()??
?
方法.?
<注>該方法繼承自 Hashtable 的clear()方法.清空哈希表.?
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
