文本文件如何转成JSON字符串
前期准备
- 引入fastJson依赖
- 读取text文件代码
public static JSONObject readText(String text){
File file = new File(text);
String jsonStr = "";
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
try{
fis = new FileInputStream(file);
isr = new InputStreamReader(fis,"utf-8");
br = new BufferedReader(isr);
String line = "";
while((line = br.readLine())!= null){
jsonStr += line;
}
return JSONObject.parseObject(jsonStr);
}catch (IOException e){
e.printStackTrace();
return null;
}finally {
try {
fis.close();
isr.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
** 注意:text文件中的数据格式必须为JSON格式**