Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自Java web应用程序的FileNotFoundException_Java_File Io_Relative Path_Fileinputstream - Fatal编程技术网

来自Java web应用程序的FileNotFoundException

来自Java web应用程序的FileNotFoundException,java,file-io,relative-path,fileinputstream,Java,File Io,Relative Path,Fileinputstream,我正在使用Eclipse开发web应用程序。我已经为数据库配置创建了一个属性文件。(DBProperty.properties) 请在下面找到文件夹结构的屏幕截图 我想访问此属性文件。我正在访问以下代码 FileInputStream input = new FileInputStream("src/resources/DBProperty.properties"); 我也尝试过许多相关的途径,但都没有成功 我已为此项目设置了生成路径。您需要使用 MyClass.class.getClass

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

我想访问此属性文件。我正在访问以下代码

FileInputStream input = new FileInputStream("src/resources/DBProperty.properties");
我也尝试过许多相关的途径,但都没有成功

我已为此项目设置了生成路径。

您需要使用

MyClass.class.getClassLoader().getResourceAsStream("DBProperty.properties")
你需要使用

MyClass.class.getClassLoader().getResourceAsStream("DBProperty.properties")
请尝试上面的代码行。希望它能解决你的问题

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

  • 运行时,
    src
    目录不在那里
  • 资源不是文件
  • 您需要查看
    Class.getResource()
    和朋友

  • 运行时,
    src
    目录不在那里
  • 资源不是文件

  • 您需要查看
    Class.getResource()
    和朋友。

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

    公共静态void main(字符串[]args){


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

    公共静态void main(字符串[]args){


    谢谢。它与getClassLoader()配合使用。getResourceAsStream()配合使用。谢谢。它与getClassLoader()配合使用。getResourceAsStream()配合使用。
        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();
        }
    }