




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第java讀取文件內(nèi)容,解析Json格式數(shù)據(jù)方式目錄java讀取文件內(nèi)容,解析Json格式數(shù)據(jù)一、讀取txt文件內(nèi)容(Json格式數(shù)據(jù))二、解析處理Json格式數(shù)據(jù)三、結(jié)果存入數(shù)據(jù)庫四、測試java讀取txt文件中的json數(shù)據(jù),進(jìn)行導(dǎo)出以下代碼可直接運行
java讀取文件內(nèi)容,解析Json格式數(shù)據(jù)
一、讀取txt文件內(nèi)容(Json格式數(shù)據(jù))
publicstaticStringreader(StringfilePath){
try{
Filefile=newFile(filePath);
if(file.isFile()file.exists()){
InputStreamReaderread=newInputStreamReader(newFileInputStream(file),"UTF-8");
BufferedReaderbufferedReader=newBufferedReader(read);
StringlineTxt=bufferedReader.readLine();
while(lineTxt!=null){
returnlineTxt;
}catch(UnsupportedEncodingException|FileNotFoundExceptione){
System.out.println("Cannotfindthefilespecified!");
e.printStackTrace();
}catch(IOExceptione){
System.out.println("Errorreadingfilecontent!");
e.printStackTrace();
returnnull;
}
二、解析處理Json格式數(shù)據(jù)
privatestaticvoidprocess(StringtxtStr){
JSONObjectjson=JSONObject.fromObject(txtStr);
JSONArraydatas=json.getJSONObject("data").getJSONArray("rows");
ListMapString,Objectlist=newArrayList();
for(inti=0;idatas.length();i++){
MapString,Objectmap=newHashMap();
JSONObjectobj=datas.getJSONObject(i).getJSONObject("cells");
Stringname=obj.getString("weibo_name");
Stringcode=obj.getString("weibo_id");
Stringurl=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);
三、結(jié)果存入數(shù)據(jù)庫
privatestaticvoidinsert(ListMapString,Objectlist){
for(MapString,Objectmap:list){
//遍歷數(shù)據(jù),寫存儲方法
}
四、測試
publicstaticvoidmain(String[]args){
StringfilePath="E:\\wugang\\data\\weiboyi\\wechat.txt";
StringtxtStr=reader(filePath);
if(txtStr!=null){
process(txtStr);
}else{
System.out.println("Readthecontentisempty!");
System.out.println("---end---");
java讀取txt文件中的json數(shù)據(jù),進(jìn)行導(dǎo)出
txt文件中的內(nèi)容如下
以下代碼可直接運行
packagecom.hwt.count.test;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.InputStreamReader;
importjava.util.ArrayList;
importjava.util.List;
importorg.apache.poi.hssf.usermodel.HSSFCell;
importorg.apache.poi.hssf.usermodel.HSSFCellStyle;
importorg.apache.poi.hssf.usermodel.HSSFFont;
importorg.apache.poi.hssf.usermodel.HSSFRow;
importorg.apache.poi.hssf.usermodel.HSSFSheet;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
importorg.apache.poi.hssf.util.CellRangeAddress;
importorg.apache.poi.hssf.util.HSSFColor;
importnet.sf.json.JSONObject;
publicclassTestaa{
publicstaticvoidmain(String[]args){
try{
Stringpath="C:/Users/dell/Desktop/test.txt";
Filefile=newFile(path);
InputStreamReaderisr=newInputStreamReader(newFileInputStream(file),"GBK");
BufferedReaderbr=newBufferedReader(isr);
Stringcontent=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=newString[]{"code_type","code","name"};
ListObject[]dataList=newArrayListObject[]
for(Stringarrc:arrContent){
JSONObjectjsonObj=JSONObject.fromObject("{"+arrc+"}");
Stringcode=jsonObj.getString("code");
Stringname=jsonObj.getString("name");
Object[]obj=newObject[rowsName.length];
obj[0]="TYPE";
obj[1]=code;
obj[2]=name;
dataList.add(obj);
//設(shè)置列頭名稱和表體數(shù)據(jù)
HSSFWorkbookworkbook=setWorkBookDate(dataList,rowsName);
try{
//將workbook對象輸出到文件test.xls
FileOutputStreamfos=newFileOutputStream("C:/Users/dell/Desktop/test.xls");
workbook.write(fos);
fos.flush();//緩沖
fos.close();//關(guān)閉流
}catch(Exceptione1){
e1.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
privatestaticHSSFWorkbooksetWorkBookDate(ListObject[]dataList,String[]rowsName){
//創(chuàng)建工作簿對象
HSSFWorkbookworkbook=newHSSFWorkbook();
//創(chuàng)建工作表,設(shè)置當(dāng)前頁名稱
HSSFSheetsheet=workbook.createSheet("測試");
//設(shè)置默認(rèn)行高
sheet.setDefaultRowHeight((short)350);
//合并表頭表尾的單元格
/*sheet.addMergedRegion(newCellRangeAddress(0,2,0,3));
sheet.addMergedRegion(newCellRangeAddress(3,3,0,3));
//凍結(jié)行
workbook.getSheetAt(0).createFreezePane(0,4);
RegionUtil.setBorderBottom(1,newCellRangeAddress(3,3,0,3),workbook.getSheetAt(0),workbook);//設(shè)置邊框
*///獲取表頭樣式對象
//獲取表體樣式對象
HSSFCellStylestyle=getCommonStyle(workbook);
//定義所需列數(shù)
intcolumnNum=rowsName.length;
//創(chuàng)建列頭
HSSFRowrowHead=sheet.createRow(0);
for(intn=0;ncolumnNum;n++){
HSSFCellcellRow=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(inti=0;idataList.size();i++){
Object[]obj=newObject[dataList.get(i).length];
obj[0]=dataList.get(i)[0];
obj[1]=dataList.get(i)[1];
obj[2]=dataList.get(i)[2];
HSSFRowrow=sheet.createRow(i+1);//創(chuàng)建所需的行數(shù)
for(intj=0;jobj.length;j++){
HSSFCellcell=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(intcolNum=0;colNumcolumnNum;colNum++){
intcolumnWidth=sheet.getColumnWidth(colNum)/256;
for(introwNum=0;rowNumsheet.getLastRowNum();rowNum++){
HSSFRowcurrentRow;
//當(dāng)前行未被使用過
if(sheet.getRow(rowNum)==null){
currentRow=sheet.createRow(rowNum);
}else{
currentRow=sheet.getRow(rowNum);
if(currentRow.getCell(colNum)!=null){
HSSFCellcurrentCell=currentRow.getCell(colNum);
if(currentCell!=null!"".equals(currentCell)){
if(currentCell.getCellType()==HSSFCell.CELL_TYPE_STRING){
intlength=currentCell.getStringCellValue().getBytes().length;
if(columnWidthlength){
columnWidth=length;
if(colNum==0){
//設(shè)置表體第一列的寬度
sheet.setColumnWidth(colNum,(columnWidth+4)*400);
}else{
//設(shè)置表體其他列的寬度
sheet.setColumnWidth(colNum,(columnWidth+4)*400);
returnworkbook;
publicstaticHSSFCellStylegetCommonStyle(HSSFWorkbookworkbook){
//設(shè)置字體
HSSFFontfont=workbook.createFont();
//設(shè)置字體大小
font.setFontHeightInPoints((short)11);
//字體加粗
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//設(shè)置字體名字
font.setFontName("CourierNew");
//設(shè)置樣式;
HSSFCellStylestyle=workbook.createCellStyle();
//設(shè)置底邊框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 計算機(jī)二級MySQL聯(lián)結(jié)查詢試題及答案
- 軟件測試中的數(shù)據(jù)處理技巧2025年試題及答案
- 網(wǎng)絡(luò)技術(shù)的職業(yè)發(fā)展規(guī)劃試題及答案
- 公共政策對職業(yè)培訓(xùn)的影響試題及答案
- 新興科技與領(lǐng)導(dǎo)戰(zhàn)略的整合探討試題及答案
- 信息系統(tǒng)考試復(fù)習(xí)中的重點知識細(xì)化試題及答案
- 信息系統(tǒng)維護(hù)過程中的有效溝通試題及答案
- 教育機(jī)構(gòu)如何利用ERP系統(tǒng)實現(xiàn)管理自動化
- 教職工的職責(zé)與挑戰(zhàn)如何確保教育領(lǐng)域的數(shù)據(jù)安全
- 港口物流自動化系統(tǒng)設(shè)計企業(yè)制定與實施新質(zhì)生產(chǎn)力項目商業(yè)計劃書
- 家校攜手決戰(zhàn)中考-九年級家長會課件
- 2022-2023學(xué)年畢節(jié)地區(qū)赫章縣六年級下學(xué)期小升初真題精選數(shù)學(xué)試卷含答案
- 幼兒園學(xué)前-《紙花開放》教學(xué)設(shè)計學(xué)情分析教材分析課后反思
- 蘇州昆山鹿城村鎮(zhèn)銀行2023年招聘人員筆試歷年難、易錯考點試題含答案附詳解
- 營養(yǎng)??谱o(hù)士理論考核試題及答案
- 山西煤炭運銷集團(tuán)錦瑞煤業(yè)有限公司煤炭資源開發(fā)利用、地質(zhì)環(huán)境保護(hù)與土地復(fù)墾方案
- 《國家中藥飲片炮制規(guī)范》全文
- 地鐵車站畢業(yè)設(shè)計
- Q-SY 06351-2020 輸氣管道計量導(dǎo)則
- 鐵路工程定額電子版(Excel版)
- 基于PLC的自動洗車系統(tǒng)設(shè)計論文
評論
0/150
提交評論