java读取⽂件内容,解析Json格式数据⽅式⽬录
java读取⽂件内容,解析Json格式数据
⼀、读取txt⽂件内容(Json格式数据)
⼆、解析处理Json格式数据
三、结果存⼊数据库
四、测试
java 读取txt⽂件中的json数据,进⾏导出
以下代码可直接运⾏
java读取⽂件内容,解析Json格式数据
⼀、读取txt⽂件内容(Json格式数据)
public static String reader(String filePath) {
try {
File file = new File(filePath);
if (file.isFile() && ists()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = adLine();
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格式数据
table tennis和pingpong
private static void process(String txtStr) {
JSONObject json = JSONObject.fromObject(txtStr);
JSONArray datas = JSONObject("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 = JSONObject(i).getJSONObject("cells");
String name = String("weibo_name");
String code = String("weibo_id");
String url = BASE_URL + String("url");
退出docker命令
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\\";
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 st;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
jsp文件怎么导出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/";
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 = place("},{", ";");
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 = String("code");
String name = String("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 = ateSheet("测试");
//设置默认⾏⾼
sheet.setDefaultRowHeight((short)350);
//合并表头表尾的单元格
/*sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 3));
sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 3));
//冻结⾏
怎么添加表单控件
RegionUtil.setBorderBottom(1, new CellRangeAddress(3, 3, 0, 3), SheetAt(0), workbook);//设置边框*/        // 获取表头样式对象
// 获取表体样式对象
HSSFCellStyle style = getCommonStyle(workbook);
// 定义所需列数
int columnNum = rowsName.length;
//创建列头
HSSFRow rowHead = ateRow(0);
for(int n = 0;n < columnNum;n++){
HSSFCell cellRow = ateCell(n,HSSFCell.CELL_TYPE_STRING);//创建列头对应个数的单元格
cellRow.setCellValue(rowsName[n]);//设置列头单元格的值
cellRow.setCellStyle(style);//设置列头单元格样式
}
//将查询出的数据设置到sheet对应的单元格中
for(int i=0;i<dataList.size();i++){
Object[] obj =new (i).length];
obj[0] = (i)[0];
obj[1] = (i)[1];
obj[2] = (i)[2];
HSSFRow row = ateRow(i+1);//创建所需的⾏数
for(int j = 0; j < obj.length; j++){
HSSFCell cell = ateCell(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 = ColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < LastRowNum(); rowNum++) {
HSSFRow currentRow;
//当前⾏未被使⽤过
if (Row(rowNum) == null) {
currentRow = ateRow(rowNum);
} else {
currentRow = Row(rowNum);
}
if (Cell(colNum) != null) {
HSSFCell currentCell = Cell(colNum);
if(currentCell != null && !"".equals(currentCell)){
if (CellType() == HSSFCell.CELL_TYPE_STRING) {python解析json文件
int length = StringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
}
if(colNum == 0){
//设置表体第⼀列的宽度
sheet.setColumnWidth(colNum, (columnWidth+4) * 400);
}else{
//设置表体其他列的宽度
函数instr的功能是字符串截取
sheet.setColumnWidth(colNum, (columnWidth+4) * 400);
}
}
return workbook;
}
public static HSSFCellStyle getCommonStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = ateFont();
//设置字体⼤⼩
font.setFontHeightInPoints((short)11);
//字体加粗
/
/font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
font.setFontName("Courier New");
//设置样式;
HSSFCellStyle style = ateCellStyle();
//设置底边框;
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;
}
}
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。