(http://bbs.csdn.net/topics/30306573)
/*属性文件:db.properties */
Properties props = new Properties();
props.load(new FileInputStream("db.properties"));
发生异常 Exception:java.io.FileNotFoundException: db.properties<系统找不到指定文件。
相对路径怎么指定?
必须用绝对路径么?如:D:\db.properties
望指教
回答:
如果在Web中,getServletContext().getResourceAsStream("/WEB-INF/db.properties")
props.load(new FileInputStream("db.properties")); 是读取当前目录的db.properties文件
getClass.getResourceAsStream("db.properties"); 是读取当前类所在位置一起的db.properties文件
getClass.getResourceAsStream("/db.properties"); 是读取ClassPath的根的db.properties文件,注意ClassPath如果是多个路径或者jar文件的,只要在任意一个路径目录下或者jar文件里的根下都可以,如果存在于多个路径下的话,按照ClassPath中的先后顺序,使用先找到的,其余忽略.