繁体   English   中英

spring 开机不走文件相对路径

[英]File relative path is not taken in spring boot

我在 spring 引导应用程序的资源目录中创建了 json 文件。 文件路径如下: resources/recipe/recipe.json当我使用相对路径时,它不会被 java 编译器采用,所以我会得到这个异常。 java.io.FileNotFoundException:recipe/recipe.json(没有这样的文件或目录)。 但此路径由 IDE 标识。如果我单击该路径,它将被重定向到 json 文件。 谁能说出问题是什么?

(仅供参考:绝对路径运行良好)

如果您的类路径中有资源文件夹,那么您应该使用 Spring 的 Resource 类并将资源位置指定为

classpath:recipe/recipe.json

您可能想阅读这个答案: Spring Boot - Reading Text File using ResourceLoader

如果您的资源存在于resources/static/listing.csv

String path = "classpath:static/listings.csv";

ResultSet rs = new Csv().read(path, null, null);

如果需要访问不在类路径中的文件,可以提供模块根目录的相对路径。

例如

new File("src/main/resouces/recipe/recipe.json");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM