java - JSON 文件未复制到目标文件夹

标签 java json intellij-idea deployment

在我的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/

相关文章:

java - 永久保存字符串的值

Java - 是否可以实例化用户定义类或java基本类型

Java - 我的代码不会向类属性返回任何非空值

java - 如何在intellij-idea中为lombok生成的方法构建调用层次结构

javascript - 我如何执行这个 AJAX/JSONP 代码?

git - 在 IntelliJ IDEA 中为现有项目添加 GIT 支持

java - 从哪里获得 Swagger 的 Bootstrap servlet?

java - Tomcat 启动忽略用于调试的 jpda 选项

json - 在 swift 中将 UUID() 与 json 一起使用

javascript - 如何在 javascript/typescript 中重新映射 json 键值映射?