这是我的文件夹项目
我想读取我项目的 web 目录中的文件 book-form.html 并将其放入字符串中。
这就是我调用函数“getFileContent”的方式:
String content = getFileContent("web/book-form.html");
这是函数:
public String getFileContent(String filePath){
String line, content = new String();
try {
File file = new File(filePath);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while((line = br.readLine()) != null){
content += line;
}
br.close();
fr.close();
} catch(IOException e){
System.out.println(e.getMessage());
}
return content;
}
我的问题是 netbeans 告诉我它找不到我的文件 book-form.html
有什么想法吗?
最佳答案
File path to resource in our war/WEB-INF folder?
此外,您应该在最后一个 block 中关闭流或使用 try-with-resource如果你使用jdk 7+
关于java - 在 netbeans + glassfish 项目中找不到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29497059/