本篇文章小编给大家分享一下java读取文件内容解析Json格式数据方式代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
一、读取txt文件内容(Json格式数据)
public static String reader(String filePath) { try { File file = new File(filePath); if (file.isFile() && file.exists()) { InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8"); BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = bufferedReader.readLine(); while (lineTxt != null) { return lineTxt; } } } catch (UnsupportedEncodingException | FileNotFoundException e) { System.out.println("Cannot find the file specified!"); e.printStackTrace(); } catch (IOException e) { System.out.println("Error reading file content!"); e.printStackTrace(); } return null; }
二、解析处理Json格式数据
private static void process(String txtStr) { JSONObject json = JSONObject.fromObject(txtStr); JSONArray datas = json.getJSONObject("data").getJSONArray("rows"); List
三、结果存入数据库
private static void insert(List
四、测试
public static void main(String[] args) { String filePath = "E:wugangdataweiboyiwechat.txt"; String txtStr = reader(filePath); if (txtStr != null) { process(txtStr); } else { System.out.println("Read the content is empty!"); } System.out.println("--- end ---"); }
java 读取txt文件中的json数据,进行导出
txt文件中的内容如下
以下代码可直接运行
package com.hwt.count.test; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.CellRangeAddress; import org.apache.poi.hssf.util.HSSFColor; import net.sf.json.JSONObject; public class Testaa { public static void main(String[] args) { try { String path = "C:/Users/dell/Desktop/test.txt"; File file = new File(path); InputStreamReader isr = new InputStreamReader(new FileInputStream(file),"GBK"); BufferedReader br = new BufferedReader(isr); String content = br.readLine() ; br.close(); isr.close(); content = content.substring(2, content.length()-2); content = content.replace("},{", ";"); String[] arrContent = content.split(";"); //设置列头名称和表体数据 String[] rowsName = new String[]{"code_type","code","name"}; List
敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载像素火影骨架佐助 (Perseverance Fire Shadow)手机版v1.16
下载要塞英雄 安卓版v33.20.0-39082670-Android
下载梦想城镇vivo最新版本 安卓版v12.0.1
梦想城镇vivo版是这款卡通风模拟经营类手游的渠道服版本,玩
怦然心动的瞬间 安卓版v1.0
怦然心动的瞬间是一款真人向的恋爱互动游戏,在游戏中玩家将扮演
曼尼汉堡店游戏 安卓版v1.0.3
曼尼汉堡店是一款非常好玩的精品恐怖类型冒险游戏,在这款游戏中
现代总统模拟器去广告版 安卓版v1.0.46
现代总统模拟器是一款休闲养成类游戏,可能对于不少的玩家来说都
现代总统模拟器付费完整版 安卓版v1.0.46
现代总统模拟器高级版在商店是需要付费的,相对于普通版本,高级