欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

JAVA屬性文件的操作類Propertise

系統(tǒng) 1858 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類的構(gòu)造函數(shù)

  示例: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()靜態(tài)方法

  示例:InputStreamin=ClassLoader.getSystemResourceAsStream(name);

  Propertiesp=newProperties();

  p.load(in);

  補(bǔ)充

  Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法

  示例:InputStreamin=context.getResourceAsStream(path);

  Propertiesp=newProperties();

  p.load(in);

?

這個(gè)類的作用在于幫你解決連接數(shù)據(jù)庫時(shí)的 " 硬編碼 " 問題 , 即驅(qū)動(dòng)類 , 連接字符串 , 用戶名 , 密碼這些信息可以通過資源文件來獲得 , 這種做法既增加了安全性 , 又使代碼容易維護(hù) .

??

處理數(shù)據(jù)庫資源文件的類 ? DBConfig. java

?

這個(gè)類的作用在于幫你解決連接數(shù)據(jù)庫時(shí)的 " 硬編碼 " 問題 , 即驅(qū)動(dòng)類 , 連接字符串 , 用戶名 , 密碼這些信息可以通過資源文件來獲得 , 這種做法既增加了安全性 , 又使代碼容易維護(hù) .

??

處理數(shù)據(jù)庫資源文件的類 ? DBConfig. java

?

Java 代碼 復(fù)制代碼
  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 代碼 復(fù)制代碼
  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

? 在連接數(shù)據(jù)庫的代碼中 , 可以通過以下方式得到驅(qū)動(dòng)類 ,url,username,password

?

Java 代碼 復(fù)制代碼
  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 代碼 復(fù)制代碼
  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.

請(qǐng)注意這三個(gè)文件的位置 , 建議放在同一個(gè)目錄下

?

一. Properties 簡(jiǎn)介?
java .util. Properties 繼承自 java .util.Hashtable?
Properties 類表示一個(gè)持久的屬性集. Properties 可保存在流中或從流中加載.屬性列表中每個(gè)鍵及其對(duì)應(yīng)值都是一個(gè)字符串.?

二.基本方法?
2.1 如何從輸入流中加載屬性文件?
使用load(InputStream is)方法:?

Java 代碼? 復(fù)制代碼
  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 代碼? 復(fù)制代碼
  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 代碼? 復(fù)制代碼
  1. Enumeration?enumeration?=? properties .propertyNames();??



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

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

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

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

Java 代碼? 復(fù)制代碼
  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 代碼? 復(fù)制代碼
  1. clear()??

?

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

?

?

JAVA屬性文件的操作類Propertise


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 国产网站在线播放 | 一区二区三区日韩视频在线观看 | 亚洲最黄视频 | 午夜影院在线观看视频 | 欧美日韩亚洲在线 | 成人欧美网站免费 | 亚洲精品综合一区二区三 | 亚洲天堂日本 | 国产熟妇久久777777 | 在线天堂中文在线资源网 | 天天插天天射天天操 | 欧美日韩综合精品一区二区三区 | 狠狠干夜夜撸 | 男女视频在线免费观看 | 天天操天天爱天天干 | 欧美一级高清免费 | 成人精品久久 | 久久福利电影 | 久久久激情视频 | 一区二区三区在线播放 | 欧美日韩高清不卡一区二区三区 | 四虎国产成人免费观看 | 婷婷六月综合 | 99国产精品久久久 | 亚洲欧美日韩综合一区久久 | 亚洲色婷婷久久精品AV蜜桃久久 | 一区不卡 | 亚洲国产精品一区二区第一页 | 久热久热| 91精品国产一区二区三区蜜臀 | 人人干操| 91亚洲精品丁香在线观看 | 国产成人综合网在线观看 | 操天天操 | 久久99热只有视精品6国产 | 首页亚洲国产丝袜长腿综合 | 91网址在线 | 久草色视频 | gvg668| 中文字幕视频在线观看 | 日本不良网站 |