亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

JAVA屬性文件的操作類Propertise

系統 1793 0

?

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

?

Java 代碼 復制代碼
  1. import ? java .util. Properties ;??
  2. import ? java .io.*;??
  3. ??
  4. public ? class ?DBConfig?{??
  5. ???? private ? static ?Object?initLock?=? new ?Object();??
  6. ??
  7. ???? private ? static ?DBConfig?dbconfig?=? null ;??
  8. ??
  9. ???? private ? Properties ?props?=? null ;??
  10. ??
  11. ???? public ? static ?DBConfig?getInstance()?{??
  12. ???????? if ?(dbconfig?==? null )?{??
  13. ???????????? synchronized ?(initLock)?{??
  14. ???????????????? if ?(dbconfig?==? null )?{??
  15. ????????????????????dbconfig?=? new ?DBConfig();??
  16. ????????????????}??
  17. ????????????}??
  18. ????????}??
  19. ???????? return ?dbconfig;??
  20. ????}??
  21. ??
  22. ???? private ? synchronized ? void ?loadProperties()?{??
  23. ????????props?=? new ? Properties ();??
  24. ???????? try ?{??
  25. ????????????System.out.println( "Load?pro?file" );??
  26. ????????????InputStream?in?=?getClass().getResourceAsStream( "/db. properties " );??
  27. ????????????props.load(in);??
  28. ????????}? catch ?(Exception?e)?{??
  29. ????????????e.printStackTrace();??
  30. ????????}??
  31. ????}??
  32. ??
  33. ???? public ?String?getProperty(String?propName)?{??
  34. ???????? if ?(props?==? null )?{??
  35. ????????????loadProperties();??
  36. ????????}??
  37. ???????? return ?props.getProperty(propName);??
  38. ????}??
  39. }??
Java 代碼 復制代碼
  1. import ? java .util. Properties ; ??
  2. import ? java .io.*; ??
  3. ??
  4. public ? class ?DBConfig?{ ??
  5. ???? private ? static ?Object?initLock?=? new ?Object(); ??
  6. ??
  7. ???? private ? static ?DBConfig?dbconfig?=? null ; ??
  8. ??
  9. ???? private ? Properties ?props?=? null ; ??
  10. ??
  11. ???? public ? static ?DBConfig?getInstance()?{ ??
  12. ???????? if ?(dbconfig?==? null )?{ ??
  13. ???????????? synchronized ?(initLock)?{ ??
  14. ???????????????? if ?(dbconfig?==? null )?{ ??
  15. ????????????????????dbconfig?=? new ?DBConfig(); ??
  16. ????????????????} ??
  17. ????????????} ??
  18. ????????} ??
  19. ???????? return ?dbconfig; ??
  20. ????} ??
  21. ??
  22. ???? private ? synchronized ? void ?loadProperties()?{ ??
  23. ????????props?=? new ? Properties (); ??
  24. ???????? try ?{ ??
  25. ????????????System.out.println( "Load?pro?file" ); ??
  26. ????????????InputStream?in?=?getClass().getResourceAsStream( "/db. properties " ); ??
  27. ????????????props.load(in); ??
  28. ????????}? catch ?(Exception?e)?{ ??
  29. ????????????e.printStackTrace(); ??
  30. ????????} ??
  31. ????} ??
  32. ??
  33. ???? public ?String?getProperty(String?propName)?{ ??
  34. ???????? if ?(props?==? null )?{ ??
  35. ????????????loadProperties(); ??
  36. ????????} ??
  37. ???????? return ?props.getProperty(propName); ??
  38. ????} ??
  39. }??
    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

?

Java 代碼 復制代碼
  1. String?driver???????=???DBConfig.getInstance().getProperty( "driver" );??
  2. String?url??????????=???DBConfig.getInstance().getProperty( "url" );??
  3. String?username?=???DBConfig.getInstance().getProperty( "username" );??
  4. String?password?=???DBConfig.getInstance().getProperty( "password" );??
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="0" height="0" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"> <param name="src" value="http://peaklui.iteye.com/javascripts/syntaxhighlighter/clipboard.swf"> <embed type="application/x-shockwave-flash" width="0" height="0" src="http://peaklui.iteye.com/javascripts/syntaxhighlighter/clipboard.swf"></embed></object>
Java 代碼 復制代碼
  1. String?driver???????=???DBConfig.getInstance().getProperty( "driver" ); ??
  2. String?url??????????=???DBConfig.getInstance().getProperty( "url" ); ??
  3. String?username?=???DBConfig.getInstance().getProperty( "username" ); ??
  4. 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)方法:?

Java 代碼? 復制代碼
  1. Properties ? properties ?=? new ? Properties ();??
  2. InputStream?is?=? new ?FileInputStream( "conn. properties " );??
  3. properties .load(is);??
  4. is.close();??



2.2 如何讀屬性文件中的值?
使用getProperties(String key)方法:

Java 代碼? 復制代碼
  1. 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()方法,該方法返回是鍵的枚舉.

Java 代碼? 復制代碼
  1. Enumeration?enumeration?=? properties .propertyNames();??



2.4 如何修改屬性文件中的值?
使用

Java 代碼? 復制代碼
  1. setProperties(String?key,?String?value)??

方法.?
<注>該方法調用的 Hashtable 的put方法.如果鍵存在,則修改值;如果鍵不存在,則添加值.?

2.5 如何存儲屬性文件到輸出流?
使用 store (OutputStream os, String description)方法:

Java 代碼? 復制代碼
  1. Properties ? properties ?=? new ? Properties ();??
  2. OutputStream?os?=? new ?FileOutputStream( "test. properties " );??
  3. String?description?=? " store ? properties ?to?test. properties " ;??
  4. properties . store (os,?description);??
  5. os.close();??



2.6 如何清空所有值?
使用

Java 代碼? 復制代碼
  1. clear()??

?

方法.?
<注>該方法繼承自 Hashtable 的clear()方法.清空哈希表.?

?

?

JAVA屬性文件的操作類Propertise


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 97伦理片| 色片在线| 99视频精品全部免费免费观 | 久久精品在线播放 | 在线播放成人毛片免费视 | 91年精品国产福利线观看久久 | 久久免费精品 | 成年人国产网站 | 欧美在线一区二区三区 | 在线99视频| 韩国19禁青草福利视频在线 | 国产精品亚洲综合第一区 | 热久久99精品这里有精品 | 亚洲精品久久中文字幕 | 亚洲最大激情中文字幕 | 天海翼一区二区三区免费 | 国产乱色 | 国产精品午夜性视频 | 爱爱免费观看高清视频在线播放 | 国产精品久久久久久久久久直 | 成人久久精品一区二区三区 | 夜夜爽www| 国产一区二区三区四 | 久久午夜一区二区 | 野花成人 | 亚洲国产日韩在线一区 | 日本香蕉网 | 国产精自产拍久久久久久 | 99在线观看免费视频 | 在线国产一区二区三区 | 毛片看看 | 久久香蕉国产线看观看网站 | 快射视频欧美 | 亚洲男人天堂视频 | 日韩在线视精品在亚洲 | 久久综合九色综合97婷婷女人 | 亚洲不卡视频在线观看 | 中文无码久久精品 | 性欧美极品xxxx欧美一区二区 | 在线观看精品视频一区二区三区 | 日韩欧美在线观看 |