`
dove19900520
  • 浏览: 593554 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java读取properties配置文件的方式总结

    博客分类:
  • java
阅读更多

java读取properties配置文件

方式一、利用Properties类加载配置文件

public static void test(){
		Properties pro = new Properties();
		InputStream is = null;
		try {
			is = PropertiesTest.class.getClassLoader().getResourceAsStream("b.properties");
			//System.out.println(is);
			pro.load(is);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 

需要注意的有两点:

1、利用PropertiesTest.class.getClassLoader().getResourceAsStream("b.properties")这种方式对其properties文件流时,不能省掉.getClassLoader(),否则会报空指针异常。

2、配置文件的存放位置,必须放在该类所在的包下,或者放在source folder文件夹下,普通的文件夹是读取不到文件的。至于原因,由于水平有限,暂未找出。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics