java 读取 properties 文件的方法

什么都不说了,直接上代码~~^_^


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));
		}
	}
}

到此结束啦~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值