程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> JAVA讀取屬性文件的幾種方法總結

JAVA讀取屬性文件的幾種方法總結

編輯:JAVA編程入門知識

1.使用java.util.Properties類的load()方法

示例:

Java代碼
InputStream in = lnew BufferedInputStream(new FileInputStream(name));  
  Properties p = new Properties();  
  p.load(in); 

2.使用java.util.ResourceBundle類的getBundle()方法 

示例:

Java代碼
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault()); 

3.使用java.util.PropertyResourceBundle類的構造函數 

示例:

Java代碼
InputStream in = new BufferedInputStream(new FileInputStream(name));  
  ResourceBundle rb = new PropertyResourceBundle(in); 

4.使用class變量的getResourceAsStream()方法 

示例:

Java代碼
InputStream in = JProperties.class.getResourceAsStream(name);  
  Properties p = new Properties();  
  p.load(in); 

5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 

示例:

Java代碼
InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);  
  Properties p = new Properties();  
  p.load(in); 

6.使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態方法 

示例:

Java代碼
InputStream in = ClassLoader.getSystemResourceAsStream(name);  
  Properties p = new Properties();  
  p.load(in); 

7.使用apache的PropertiesConfiguration類

示例:
Java代碼
Configuration config = new PropertiesConfiguration("test.properties");
config.getProperty(key);

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved