什么都不说了,直接上代码~~^_^
package com.java.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import org.junit.Test;
/**
* 读取 properties 文件的方法
* @author Administrator
*
*/
public class ReadPropertiesUtil {
/**
* 使用java.util.Properties类的load()方法
* @throws Exception
*/
@Test
public void testPropertiesLoad() throws Exception{
InputStream inStream = new BufferedInputStream(new FileInputStream("D:\\jdbc.properties"));
Properties p = new Properties();
p.load(inStream);
Enumeration<Object> keys = p.keys();
while(keys.hasMoreElements()){
String key = (String)keys.nextElement();
System.out.println("key:"+key+"----这是分隔线----value:"+p.getProperty(key));
}
}
/**
* 使用java.util.ResourceBundle类的getBundle()方法
*/
@Test
public void testResourceBundleGetBundle(){
// jdbc.properties 文件放在 src 根目录下
ResourceBundle rb = ResourceBundle.getBundle("jdbc",Locale.getDefault());
Enumeration<String> keys = rb.getKeys();
while(keys.hasMoreElements()){
String key = keys.nextElement();
System.out.println("key:"+key+"----这是分隔线----value:"+rb.getString(key));
}
}
/**
* 使用java.util.PropertyResourceBundle类的构造函数
* @throws Exception
*/
@Test
public void testPropertyResourceBundle() throws Exception{
InputStream in = new BufferedInputStream(new FileInputStream("D:\\jdbc.properties"));
ResourceBundle rb = new PropertyResourceBundle(in);
Enumeration<String> keys = rb.getKeys();
while(keys.hasMoreElements()){
String key = keys.nextElement();
System.out.println("key:"+key+"----这是分隔线----value:"+rb.getString(key));
}
}
/**
* 使用class变量的getResourceAsStream()方法
* @throws Exception
*
* 注意:
* 1、使用 this.getClass().getClassLoader() 获取类加载器的时候会返回null,所以会报空指针异常
* 2、使用 Properties.class.getClassLoader() 会出现和 1 一样的情况
*
* 所以不建议使用这两者
*/
@Test
public void testGetResourceAsStream() throws Exception{
// InputStream in = Properties.class.getClassLoader().getResourceAsStream("/jdbc.properties");
// InputStream in = this.getClass().getClassLoader().getResourceAsStream("/jdbc.properties");
// InputStream in = this.getClass().getResourceAsStream("/jdbc.properties");
InputStream in = Properties.class.getResourceAsStream("/jdbc.properties");
Properties p = new Properties();
p.load(in);
Enumeration<Object> keys = p.keys();
while(keys.hasMoreElements()){
String key = (String)keys.nextElement();
System.out.println("key:"+key+"----这是分隔线----value:"+p.getProperty(key));
}
}
/**
* 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
* @throws IOException
*/
@Test
public void testGetSystemResourceAsStream() throws IOException{
InputStream in = ClassLoader.getSystemResourceAsStream("jdbc.properties");
Properties p = new Properties();
p.load(in);
Enumeration<Object> keys = p.keys();
while(keys.hasMoreElements()){
String key = (String)keys.nextElement();
System.out.println("key:"+key+"----这是分隔线----value:"+p.getProperty(key));
}
}
}
到此结束啦~~