java.io.FileNotFoundException:db.properties(系统找不到指定的路径)

时间:2014-10-23 13:34:06

标签: java jsp

我编写了一个基本的jsp代码,用于存储和检索db中的数据。 在此之前我正在检查用户的验证。

当我点击提交按钮时,它会重定向到我的jsp页面。

我已经单独编写了一个db.properties文件。

当我提供读取属性文件的完整路径时,程序正在执行。 (这不是下面硬编码的最佳方式)。

FileInputStream in = new FileInputStream(" C:\ Program Files \ Apache Software Foundation \ Tomcat 6.0 \ webapps \ ServiceDisplay \ db.properties");

但是当我只指定" db.properties"喜欢 (FileInputStream in = new FileInputStream(" db.properties");)程序没有执行我得到文件未找到异常

请不要将此属性文件放在当前工作目录中。 (即我的db.properties文件和我的jsp文件位于 ServiceDisplay

我尝试将文件名更改为" // db.properties"," /db.properties"," ./ db.properties",& #34; \ db.properties",。\ db.properties,.. / db.properties"," .. \ db.properties" 。 但我仍然得到 java.io.FileNotFoundException:db.properties

1 个答案:

答案 0 :(得分:0)

文件'ServiceDisplay \ db.properties'在您的资源目录中,要加载此文件,必须使用getResoursceAsStream。代码示例如下:

 ServletContext context = request.getSession().getServletContext();
 InputStream is = context.getResourceAsStream("/db.properties");