/**
* 类装载器读取资源文件
* @author Administrator
*
*/
//web服务器运行时,其资源文件在classes路径下的文件可以和其他字节码文件一样被类装载器装载
public class ReadFileDao {
<span style="white-space:pre"> </span>private static Properties pros;
<span style="white-space:pre"> </span>static{
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>InputStream in = ReadFileDao.class.getClassLoader().getResourceAsStream("/db.properties");
<span style="white-space:pre"> </span>pros = new Properties();
<span style="white-space:pre"> </span>pros.load(in);
<span style="white-space:pre"> </span>catch (IOException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void update() throws IOException{
<span style="white-space:pre"> </span>//类装载器指向的根目录为:/classes(编译后的src目录。即为classes目录)
<span style="white-space:pre"> </span>String username = pros.getProperty("username");
<span style="white-space:pre"> </span>}
}
必背程序-javaWebDao层读取资源文件
最新推荐文章于 2021-05-31 22:25:43 发布