}
普通类获取资源文件
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.junit.Test;
public class demo {
@Test
public void show() throws IOException{
//不会更新
// InputStream in = demo.class.getClassLoader().getResourceAsStream(".."+File.separator+".."+File.separator+"file/file.properties");
// Properties pro = new Properties();
// pro.load(in);
// String url = pro.getProperty("url");
// System.out.println(url);
//会更新的
// String path = demo.class.getClassLoader().getResource(".."+File.separator+".."+File.separator+"file/file.properties").getPath();
// Properties pro = new Properties();
// FileInputStream in = new FileInputStream(path);
// pro.load(in);
// String url = pro.getProperty("url");
// System.out.println(url);
//db.properties 必须在 WEB-INF\classes 目录下才可以直接访问。
// String path = demo.class.getClassLoader().getResource("db.properties").getPath();
// Properties pro = new Properties();
// FileInputStream in = new FileInputStream(path);
// pro.load(in);
// String url = pro.getProperty("url");
// System.out.println(url);
InputStream in = demo.class.getClassLoader().getResourceAsStream("db.properties");
Properties pro = new Properties();
pro.load(in);
String name = pro.getProperty("url");
System.out.println(name);
}
}
servlet和普通类获取资源文件的方法,布布扣,bubuko.com
原文地址:http://blog.csdn.net/u013032887/article/details/37598213