1、第一种读取与jar包在同一个目录中的文件,也就是没打包到jar包里面
读取方式:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.core.io.ClassPathResource;
String filePath = "./data.json";
ClassPathResource classPath = new ClassPathResource(filePath);
JSONObject jsonObject = JSON.parseObject(classPath.getInputStream(),JSONObject.class);
System.out.println(jsonObject);
2、第二种读取将文件打包到jar里面的情况
文件放在resurces里面
String filePath = "./data.json";
InputStream inputStream = getClass().getResourceAsStream(filePath);
JSONObject jsonObject = JSON.parseObject(inputStream,JSONObject.class);
System.out.println(jsonObject);
两种方式