java - 杰克逊 writeValue() 到文件不使用相对路径
问题描述
我正在尝试通过杰克逊将对象写入 json 文件。
如果我提供一个绝对路径"D:/Projects/quiz-red/src/main/resources/com/models/Quizzes.json"
它正在工作并且文件出现在目录中
但是,如果我提供相对路径 -"/com/models/Quizzes.json"
我只是进入Process finished with exit code 0
控制台,没有任何反应。我究竟做错了什么?
有我的代码:
public static void writeEntityToJson(Object jsonDataObject, String path) throws IOException {
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
writer.writeValue(new File(path), jsonDataObject);
}
public static void main(String[] args) throws IOException {
Quiz quiz = new Quiz(5L, "Title", "Short desc");
writeEntityToJson(quiz, "/com/models/Quizzes2.json");
}
我想使用相对路径将文件保存到 DataProvider 中的资源
例外:
Exception in thread "main" java.io.FileNotFoundException: com\models\Quizzes5.json (The system cannot find the path specified)
at java.base/java.io.FileOutputStream.open0(Native Method)
at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:187)
at com.fasterxml.jackson.core.JsonFactory.createGenerator(JsonFactory.java:1223)
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:942)
at com.utils.DataProvider.writeEntityToJson(DataProvider.java:33)
at com.utils.DataProvider.main(DataProvider.java:50)
解决方案
推荐阅读
- sql-server - 在 SQL Server 中,我是否必须使用 exec sp_addrolemember 向用户授予角色?
- python - 由于 PIL 无法加载图像
- python - 如何将使用 ids 以行分隔数据的文本文件转换为保留空白/行的简单查找(字典、json 等)?
- java - x== -x (true) 的 x 的 22 个可能值是多少?
- java - 使用 XChart ChartPie 作为带有 PDFBoxGraphics2D 和 Salamander 的 SVG 资源的空 PDF
- animation - 嗨,我想在 react native 中制作一个如下所示的屏幕,它使用从一点到另一点的动画
- file - 目录(文件夹)中的基于文件的队列是个好主意吗?
- mongodb - mongodb 从数组中查找,然后通过引用从对象 id 中查找
- reactjs - 如何自定义反应材质菜单行为
- windows - 如何从cmder中的makefile打印颜色?