【发布时间】:2019-08-25 00:58:21
【问题描述】:
我正在设置将字符串写入 html 文件的逻辑
我尝试阅读位于 \src\main\resources\static\ 中的 details.html 文件
File htmlTemplateFile = new File("details.html");
或
File htmlTemplateFile = new File("\src\main\resources\static\details.html");
当我运行此代码时,我得到了 file not found 异常,尽管该文件位于 \src\main\resources\static\
我已经尝试了上述两种方法,仍然抛出未找到文件的异常。
错误:msgjava.io.FileNotFoundException:文件“details.html”不存在
【问题讨论】:
-
您需要绝对路径(来自根目录)或相对路径(来自执行上下文),您都没有。如果你想将它作为 resource 打开,那么你应该这样做,并且路径将相对于类路径。
-
我正在探索新鲜的java,很抱歉有疑问但是,我在哪里可以找到我的相对路径? @戴夫牛顿
-
尝试
new File("static/details.html");/resources下的所有内容都被视为类路径条目。
标签: spring spring-boot jakarta-ee file-io