【问题标题】:java.io.FileNotFoundException: db.properties (The system cannot find the path specified)java.io.FileNotFoundException: db.properties (系统找不到指定的路径)
【发布时间】:2014-12-19 04:39:41
【问题描述】:

我已经编写了一个基本的 jsp 代码来存储和检索数据库中的数据。 在此之前检查用户的验证。

当我点击提交按钮时,它会重定向到我的 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”、“\db.properties”、.\db.properties、../ db.properties", "..\db.properties" 。 但我仍然得到 java.io.FileNotFoundException: db.properties

【问题讨论】:

  • 你能把文件添加到你的类路径吗?

标签: java jsp


【解决方案1】:

文件“ServiceDisplay\db.properties”在您的资源目录中,要加载此文件需要使用getResoursceAsStream。下面的代码示例:

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

【讨论】:

    猜你喜欢
    • 2014-01-11
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 2012-05-28
    • 2017-11-21
    • 1970-01-01
    相关资源
    最近更新 更多