来自Java web应用程序的FileNotFoundException
我正在使用Eclipse开发web应用程序。我已经为数据库配置创建了一个属性文件。(DBProperty.properties) 请在下面找到文件夹结构的屏幕截图 我想访问此属性文件。我正在访问以下代码来自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
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();
}
}