使用J2SE API读取Properties文件的六种方

时间:2014-01-26 14:30:49   收藏:0   阅读:481

 

1.使用Java.util.Properties类的load()方法 

示例:

InputStream in = lnew BufferedInputStream(new FileInputStream(name));  

Properties p = new Properties(); 

p.load(in); 

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

示例

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

3.使用java.util.PropertyResourceBundle类的构造函数

示例:

InputStream in = new BufferedInputStream(new FileInputStream(name)); 

ResourceBundle rb = new PropertyResourceBundle(in);  

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

示例:

InputStream in = JProperties.class.getResourceAsStream(name); 

Properties p = new Properties(); 

p.load(in);  

5.

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

示例:

 InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name); 

 Properties p = new Properties(); 

 p.load(in); 

6.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法

 示例:

InputStream in = ClassLoader.getSystemResourceAsStream(name); 

 Properties p = new Properties(); 

 p.load(in); 

 

原文:http://www.cnblogs.com/Wen-yu-jing/p/3533708.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!