Get the App
SLTechnology News&Howtos  ›  Development  › 

How java reads the contents of files and parses data in Json format

Shulou Source: shulou.com Published: 2022-06-03 17:58:54 09月18日 Update

This article mainly introduces how java reads the contents of the file and parses the data in Json format, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

Java reads file contents and parses Json format data 1. Read txt file contents (Json format data) 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;} II. Parsing Json format data private static void process (String txtStr) {JSONObject json = JSONObject.fromObject (txtStr); JSONArray datas = json.getJSONObject ("data"). GetJSONArray ("rows"); List list = new ArrayList () For (int I = 0; I < datas.length (); iTunes +) {Map 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) Third, the result is stored in the database private static void insert (List list) {for (Map map: list) {/ / traversal data, write storage method}} IV. Test 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 reads json data in the txt file for export

The contents of the txt file are as follows

The following code can be run directly

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 (";"); / / set column header name and table body data String [] rowsName = new String [] {"code_type", "code", "name"}; List dataList = new ArrayList () 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);} / / set column header name and table body data HSSFWorkbook workbook = setWorkBookDate (dataList,rowsName) Try {/ / output workbook objects to the file test.xls FileOutputStream fos = new FileOutputStream ("C:/Users/dell/Desktop/test.xls"); workbook.write (fos); fos.flush (); / / buffer fos.close () / / close flow} catch (Exception E1) {e1.printStackTrace ();}} catch (Exception e) {e.printStackTrace ();}} private static HSSFWorkbook setWorkBookDate (List dataList,String [] rowsName) {/ / create workbook object HSSFWorkbook workbook = new HSSFWorkbook () / / create worksheet, set current page name HSSFSheet sheet = workbook.createSheet ("test"); / / set default row height sheet.setDefaultRowHeight ((short) 350); / / merge header and footer cells / * sheet.addMergedRegion (new CellRangeAddress (0,2,0,3); sheet.addMergedRegion (new CellRangeAddress (3,3,0,3) / / freeze row workbook.getSheetAt (0) .createFreezePane (0,4); RegionUtil.setBorderBottom (1, new CellRangeAddress (3,3,0,3), workbook.getSheetAt (0), workbook); / / set border * / / get header style object / / get table body style object HSSFCellStyle style = getCommonStyle (workbook) / / define the number of columns required int columnNum = rowsName.length; / / create the column header HSSFRow rowHead = sheet.createRow (0); for (int n = 0 / n < columnNum;n++) {HSSFCell cellRow = rowHead.createCell (nMed HSSFCell.CELLTYPERING string) / / create a cell cellRow.setCellValue (rowsname [n]) that corresponds to the number of column headers; / / set the value of the column header cell cellRow.setCellStyle (style) / / set the cell style of the column header} / / set the queried data to the cell corresponding to sheet for (int iTuno

Tags: Data unit style file content format font border object article color method right header width header work test number code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Redmi Linux OPPO Reno