import java.io.*;
public class Output {
//测试
public static void main(String[] args){
String json = "null";
try {
json = readJsonData("I:\\History_Project\\echarts\\life-expectancy.json");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(json);
}
public static String readJsonData(String pactFile) throws IOException {
// 读取文件数据
//System.out.println("读取文件数据util");
StringBuffer strbuffer = new StringBuffer();
File myFile = new File(pactFile);//"D:"+File.separatorChar+"DStores.json"
if (!myFile.exists()) {
System.err.println("Can't Find " + pactFile);
}
try {
FileInputStream fis = new FileInputStream(pactFile);
InputStreamReader inputStreamReader = new InputStreamReader(fis, "UTF-8");
BufferedReader in = new BufferedReader(inputStreamReader);
String str;
while ((str = in.readLine()) != null) {
strbuffer.append(str); //new String(str,"UTF-8")
}
in.close();
} catch (IOException e) {
e.getStackTrace();
}
//System.out.println("读取文件结束util");
return strbuffer.toString();
}
}
java读取json文件并转换为String
最新推荐文章于 2023-11-04 10:18:38 发布
本文介绍了一个使用Java读取JSON文件的具体示例,展示了如何通过FileInputStream和BufferedReader结合使用来读取指定路径下的JSON文件,并将读取到的数据转换为字符串输出。
3509

被折叠的 条评论
为什么被折叠?



