java读取properties文件内容

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_25218095/article/details/54918088
@Test
	public void test2() throws Exception{
		//第一种
		ClassLoader c = this.getClass().getClassLoader();
		String s ="com\\beijing\\haha\\jdbc.properties";
		InputStream in = c.getResourceAsStream(s);
		Properties p = new Properties();
		p.load(in);
		System.out.println(p.getProperty("user"));
		//第二种
		FileInputStream fi = new FileInputStream(new File("jdbc.properties"));
		Properties p2 = new Properties();
		p2.load(fi);
		System.out.println(p2.getProperty("user"));
	}
new FIle如果写相对路径的话,那么默认是项目根目录,和src同一路径

猜你喜欢

转载自blog.csdn.net/qq_25218095/article/details/54918088