java读取文件内容,解析Json格式数据方式
程序员文章站
2022-06-28 23:27:03
目录java读取文件内容,解析json格式数据一、读取txt文件内容(json格式数据)二、解析处理json格式数据java读取文件内容,解析json格式数据一、读取txt文件内容(json格式数据)...
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<map<string, object>> list = new arraylist<>(); for (int i = 0; i < datas.length(); i++) { map<string, object> map = new hashmap<>(); jsonobject obj = datas.getjsonobject(i).getjsonobject("cells"); string name = obj.getstring("weibo_name"); string code = obj.getstring("weibo_id"); string url = base_url + obj.getstring("url"); map.put("name", name); map.put("code", code); map.put("url", url); list.add(map); } if (!list.isempty()) { insert(list); } }
三、结果存入数据库
private static void insert(list<map<string, object>> list) { for (map<string, object> map : list) { //遍历数据,写存储方法 } }
四、测试
public static void main(string[] args) { string filepath = "e:\\wugang\\data\\weiboyi\\wechat.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<object[]> datalist = new arraylist<object[]>(); for(string arrc : arrcontent){ jsonobject jsonobj = jsonobject.fromobject("{"+arrc+"}"); string code = jsonobj.getstring("code"); string name = jsonobj.getstring("name"); object[] obj = new object[rowsname.length]; obj[0] = "type"; obj[1] = code; obj[2] = name; datalist.add(obj); } //设置列头名称和表体数据 hssfworkbook workbook = setworkbookdate(datalist,rowsname); try { // 将workbook对象输出到文件test.xls fileoutputstream fos = new fileoutputstream("c:/users/dell/desktop/test.xls"); workbook.write(fos); fos.flush(); // 缓冲 fos.close(); // 关闭流 }catch (exception e1) { e1.printstacktrace(); } } catch (exception e) { e.printstacktrace(); } } private static hssfworkbook setworkbookdate(list<object[]> datalist,string[] rowsname){ //创建工作簿对象 hssfworkbook workbook = new hssfworkbook(); //创建工作表,设置当前页名称 hssfsheet sheet = workbook.createsheet("测试"); //设置默认行高 sheet.setdefaultrowheight((short)350); //合并表头表尾的单元格 /*sheet.addmergedregion(new cellrangeaddress(0, 2, 0, 3)); sheet.addmergedregion(new cellrangeaddress(3, 3, 0, 3)); //冻结行 workbook.getsheetat(0).createfreezepane(0, 4); regionutil.setborderbottom(1, new cellrangeaddress(3, 3, 0, 3), workbook.getsheetat(0), workbook);//设置边框 */ // 获取表头样式对象 // 获取表体样式对象 hssfcellstyle style = getcommonstyle(workbook); // 定义所需列数 int columnnum = rowsname.length; //创建列头 hssfrow rowhead = sheet.createrow(0); for(int n = 0;n < columnnum;n++){ hssfcell cellrow = rowhead.createcell(n,hssfcell.cell_type_string);//创建列头对应个数的单元格 cellrow.setcellvalue(rowsname[n]);//设置列头单元格的值 cellrow.setcellstyle(style);//设置列头单元格样式 } //将查询出的数据设置到sheet对应的单元格中 for(int i=0;i<datalist.size();i++){ object[] obj =new object[datalist.get(i).length]; obj[0] = datalist.get(i)[0]; obj[1] = datalist.get(i)[1]; obj[2] = datalist.get(i)[2]; hssfrow row = sheet.createrow(i+1);//创建所需的行数 for(int j = 0; j < obj.length; j++){ hssfcell cell = row.createcell(j,hssfcell.cell_type_string);//设置单元格的数据类型 if(!"".equals(obj[j]) && obj[j] != null){ cell.setcellvalue(obj[j].tostring());//设置单元格的值 }else{ cell.setcellvalue("");//设置单元格的值为空字符串 } cell.setcellstyle(style);//设置单元格样式 } } //让列宽随着导出的列长自动适应 for (int colnum = 0; colnum < columnnum; colnum++) { int columnwidth = sheet.getcolumnwidth(colnum) / 256; for (int rownum = 0; rownum < sheet.getlastrownum(); rownum++) { hssfrow currentrow; //当前行未被使用过 if (sheet.getrow(rownum) == null) { currentrow = sheet.createrow(rownum); } else { currentrow = sheet.getrow(rownum); } if (currentrow.getcell(colnum) != null) { hssfcell currentcell = currentrow.getcell(colnum); if(currentcell != null && !"".equals(currentcell)){ if (currentcell.getcelltype() == hssfcell.cell_type_string) { int length = currentcell.getstringcellvalue().getbytes().length; if (columnwidth < length) { columnwidth = length; } } } } } if(colnum == 0){ //设置表体第一列的宽度 sheet.setcolumnwidth(colnum, (columnwidth+4) * 400); }else{ //设置表体其他列的宽度 sheet.setcolumnwidth(colnum, (columnwidth+4) * 400); } } return workbook; } public static hssfcellstyle getcommonstyle(hssfworkbook workbook) { // 设置字体 hssffont font = workbook.createfont(); //设置字体大小 font.setfontheightinpoints((short)11); //字体加粗 //font.setboldweight(hssffont.boldweight_bold); //设置字体名字 font.setfontname("courier new"); //设置样式; hssfcellstyle style = workbook.createcellstyle(); //设置底边框; style.setborderbottom(hssfcellstyle.border_thin); //设置底边框颜色; style.setbottombordercolor(hssfcolor.black.index); //设置左边框; style.setborderleft(hssfcellstyle.border_thin); //设置左边框颜色; style.setleftbordercolor(hssfcolor.black.index); //设置右边框; style.setborderright(hssfcellstyle.border_thin); //设置右边框颜色; style.setrightbordercolor(hssfcolor.black.index); //设置顶边框; style.setbordertop(hssfcellstyle.border_thin); //设置顶边框颜色; style.settopbordercolor(hssfcolor.black.index); //在样式用应用设置的字体; style.setfont(font); //设置自动换行; style.setwraptext(false); //设置水平对齐的样式为居中对齐; style.setalignment(hssfcellstyle.align_center); //设置垂直对齐的样式为居中对齐; style.setverticalalignment(hssfcellstyle.vertical_center); return style; } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
上一篇: Linux运维基础httpd静态网页教程
下一篇: MySQL数据库体系架构详情