您好,登錄后才能下訂單哦!
這篇文章主要介紹了java如何讀取文件內(nèi)容和解析Json格式數(shù)據(jù)方式,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
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; }
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) { //遍歷數(shù)據(jù),寫存儲方法 } }
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 ---"); }
txt文件中的內(nèi)容如下
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(";"); //設(shè)置列頭名稱和表體數(shù)據(jù) 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); } //設(shè)置列頭名稱和表體數(shù)據(jù) 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(); // 關(guān)閉流 }catch (Exception e1) { e1.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } } private static HSSFWorkbook setWorkBookDate(List<Object[]> dataList,String[] rowsName){ //創(chuàng)建工作簿對象 HSSFWorkbook workbook = new HSSFWorkbook(); //創(chuàng)建工作表,設(shè)置當(dāng)前頁名稱 HSSFSheet sheet = workbook.createSheet("測試"); //設(shè)置默認(rèn)行高 sheet.setDefaultRowHeight((short)350); //合并表頭表尾的單元格 /*sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 3)); sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 3)); //凍結(jié)行 workbook.getSheetAt(0).createFreezePane(0, 4); RegionUtil.setBorderBottom(1, new CellRangeAddress(3, 3, 0, 3), workbook.getSheetAt(0), workbook);//設(shè)置邊框 */ // 獲取表頭樣式對象 // 獲取表體樣式對象 HSSFCellStyle style = getCommonStyle(workbook); // 定義所需列數(shù) int columnNum = rowsName.length; //創(chuàng)建列頭 HSSFRow rowHead = sheet.createRow(0); for(int n = 0;n < columnNum;n++){ HSSFCell cellRow = rowHead.createCell(n,HSSFCell.CELL_TYPE_STRING);//創(chuàng)建列頭對應(yīng)個數(shù)的單元格 cellRow.setCellValue(rowsName[n]);//設(shè)置列頭單元格的值 cellRow.setCellStyle(style);//設(shè)置列頭單元格樣式 } //將查詢出的數(shù)據(jù)設(shè)置到sheet對應(yīng)的單元格中 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);//創(chuàng)建所需的行數(shù) for(int j = 0; j < obj.length; j++){ HSSFCell cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);//設(shè)置單元格的數(shù)據(jù)類型 if(!"".equals(obj[j]) && obj[j] != null){ cell.setCellValue(obj[j].toString());//設(shè)置單元格的值 }else{ cell.setCellValue("");//設(shè)置單元格的值為空字符串 } cell.setCellStyle(style);//設(shè)置單元格樣式 } } //讓列寬隨著導(dǎo)出的列長自動適應(yīng) for (int colNum = 0; colNum < columnNum; colNum++) { int columnWidth = sheet.getColumnWidth(colNum) / 256; for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { HSSFRow currentRow; //當(dāng)前行未被使用過 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){ //設(shè)置表體第一列的寬度 sheet.setColumnWidth(colNum, (columnWidth+4) * 400); }else{ //設(shè)置表體其他列的寬度 sheet.setColumnWidth(colNum, (columnWidth+4) * 400); } } return workbook; } public static HSSFCellStyle getCommonStyle(HSSFWorkbook workbook) { // 設(shè)置字體 HSSFFont font = workbook.createFont(); //設(shè)置字體大小 font.setFontHeightInPoints((short)11); //字體加粗 //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //設(shè)置字體名字 font.setFontName("Courier New"); //設(shè)置樣式; HSSFCellStyle style = workbook.createCellStyle(); //設(shè)置底邊框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //設(shè)置底邊框顏色; style.setBottomBorderColor(HSSFColor.BLACK.index); //設(shè)置左邊框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //設(shè)置左邊框顏色; style.setLeftBorderColor(HSSFColor.BLACK.index); //設(shè)置右邊框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //設(shè)置右邊框顏色; style.setRightBorderColor(HSSFColor.BLACK.index); //設(shè)置頂邊框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //設(shè)置頂邊框顏色; style.setTopBorderColor(HSSFColor.BLACK.index); //在樣式用應(yīng)用設(shè)置的字體; style.setFont(font); //設(shè)置自動換行; style.setWrapText(false); //設(shè)置水平對齊的樣式為居中對齊; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //設(shè)置垂直對齊的樣式為居中對齊; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“java如何讀取文件內(nèi)容和解析Json格式數(shù)據(jù)方式”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。