@Test
public void test01() {
JSONArray jsonObjFromResource = getJsonObjFromResource("/face.json");
System.out.println(jsonObjFromResource);
}
public JSONArray getJsonObjFromResource(String filename) {
JSONArray json = null;
if (!filename.contains(".json")) {
filename += ".json";
}
try {
InputStream inputStream = getClass().getResourceAsStream(filename);
if (inputStream != null) {
StringBuilder sb = new StringBuilder();
InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader bfReader = new BufferedReader(reader);
String content = null;
while ((content = bfReader.readLine()) != null) {
sb.append(content);
}
bfReader.close();
json = JSON.parseArray(sb.toString());
} else {
log.info("[{}] file not exist", filename);
}
} catch (Exception e) {
log.error("read file to string error", e);
}
return json;
}
读取resource路径下面的json文件
最新推荐文章于 2024-05-22 15:46:18 发布