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

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文件夹下,普通的文件夹是读取不到文件的。至于原因,由于水平有限,暂未找出。

 

你可能感兴趣的:(java,properties,读取)