在我的java项目中,我有一个.json文件,我已将其放置在资源文件夹中。
当我尝试使用 getResourceAsStream()
方法在主类中读取此 .json 文件时,我收到 FileNotFoundException
。
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(fileName);
我检查了文件夹target>classes
下这个.json文件不存在。
我不确定为什么这个 .json 文件没有被复制到目标文件夹。需要帮助才能理解原因。这是一个 Maven 项目,我使用 IntelliJ 来构建和运行该项目。
最佳答案
JSON 文件未列为 IntelliJ 将视为资源的默认格式。您是否将文件格式添加到部署配置中?我将引用 IntelliJ 文档:
When building an application, IntelliJ IDEA copies all resources into the output directory, preserving the directory structure of the resources relative to the source path. The following file types are recognized as resources by default:
- .dtd
- .jpeg
- .properties
- .gif
- .jpg
- .tld
- .html
- .png
- .xml
The pattern of recognized resource files can be configured as a regular expression in the Compiler dialog (Settings/Preferences Ctrl+Alt+S | Build, Execution, Deployment | Compiler). Using the Resource Patterns field, you can add your own file extensions and create custom list of resources.
https://www.jetbrains.com/help/idea/resource-files.html
为 JSON 文件格式添加正则表达式,应该没问题。
关于java - JSON 文件未复制到目标文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67688573/