前言
读取文件——>转换为字符串——>生成实体类
一、具体实现
1、前提引入jar:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version> <!-- 使用你需要的版本 -->
</dependency>
2、代码:
读取文件:这里涉及到相对路径和绝对路径的问题
相对路径:
import java.io.InputStream;
import java.util.Scanner;
public class RelativePathExample {
public static void main(String[] args) {
String relativePath = "文件/文本-case.json"; // 注意这里不需要src/main/resources前缀
InputStream inputStream = RelativePathExample.class.getClassLoader().getResourceAsStream(relativePath);
if (inputStream != null) {
try (Scanner scanner = new Scanner(inputStream, "UTF-8")) {
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine