来自 Java Web 应用程序的 FileNotFoundException

FileNotFoundException coming from Java web application

我正在使用 Eclipse 开发 Web 应用程序。我已经为数据库配置创建了一个 属性 文件。 (DBProperty.properties) 请在下面找到文件夹结构的屏幕截图。

我想访问这个 属性 文件。我正在使用以下代码访问。

FileInputStream input = new FileInputStream("src/resources/DBProperty.properties");

我也试过很多相对路径都没有成功。

我已经为这个项目设置了构建路径。

您需要使用

MyClass.class.getClassLoader().getResourceAsStream("DBProperty.properties")
FileInputStream input = new FileInputStream("resources/DBProperty.properties");

请尝试上面的代码行。希望能解决你的问题。

  1. src 目录在运行时不存在。
  2. 资源不是文件。

你需要调查 Class.getResource() 和朋友。

您必须使用 File 对象指定完整的文件路径。

public static void main(String[] args) {

    File file = new File("C:\Path\workspace\jbossmqimpl\Test1\resources\NewFile.xml");

    try (FileInputStream fis = new FileInputStream(file)) {

        System.out.println("Total file size to read (in bytes) : "+ fis.available());

        int content;
        while ((content = fis.read()) != -1) {
            // convert to char and display it
            System.out.print((char) content);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}