在不是servlet的普通Java文件中怎么获取配置文件的信息

一、使用类装载器获取文件配置文件信息: 注意以下两点:1、通过类装载器获取的文件不能太大。2、通过类装载器获取的配置文件信息不能得到修改后的最新信息。

	public void test() throws IOException{
		InputStream in=ClassloderTest.class.getClassLoader().getResourceAsStream("db.properties");
		Properties p=new Properties();
		p.load(in);
		p.getProperty("DRIVER");
		if(in!=null){
			in.close();
		}
	}
 

 二、使用类装载至获取文件的路径获取配置文件信息:解决了用类加载器的获取文件信息的两个缺点。

 

public void test1() throws IOException{
		URL url=ClassloderTest.class.getClassLoader().getResource("db.properties");
		String path=url.getPath();
		FileInputStream fis=new FileInputStream(path);
		Properties p=new Properties();
		p.load(fis);
		p.getProperty("DRIVER");
		if(fis!=null){
			fis.close();
		}
	}
 
 

你可能感兴趣的:(servlet)