java实现从json文件中读取数据
import java.io.*;
import java.nio.charset.StandardCharsets;
/**
* @Description 文件工具类
* @ClassName FileUtils
* @Author yuhuofei
* @Date 2022/3/18 0:00
* @Version 1.0
*/
public class FileUtils {
/**
* 从json文件中读取数据
*
* @param jsonFilePath
* @return
*/
public static String getDataFromJsonFile(String jsonFilePath) {
StringBuilder jsonString = new StringBuilder();
File file = new File(jsonFilePath);
if (file.exists()) {
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
char[] chars = new char[8192];
int length;
while ((length = bufferedReader.read(chars)) != -1) {
String temp = new String(chars, 0, length);
jsonString.append(temp);
}
} catch (IOException e) {
System.out.println("=====获取数据异常=====" + e);
} finally {
if (null != bufferedReader) {
try {
bufferedReader.close();
} catch (IOException ex) {
System.out.println("=======获取数据时,关闭流异常=======" + ex);
}
}
}
}
return jsonString.toString();
}
}