Java编程之文件格式解析

一.json格式解析

 1.json字符串<-->Java对象:取的是类中有的属性,类中没有的不取,类中多出来的值为默认值

String json1="{\"name\":\"scarlett\",\"age\":\"22\",\"pwd\":\"123456\"}";
Student s= JSONObject.parseObject(json1,Student.class);            //键取的是类中有的属性,类中没有的不取,类中多出来的值为默认值
System.out.println(s);
System.out.println(JSONObject.toJSONString(s));      //对象转json字符串

2.json<-->map互转

Map<String,Object> map=JSONObject.parseObject(json1,Map.class);   //json字符串转map
System.out.println(map);
System.out.println(JSONObject.toJSONString(map));     //map转json字符串

3.json字符串<-->Java数组(list)

String json2="[{\"name\":\"scarlett\",\"age\":\"22\",\"pwd\":\"123456\"},{\"name\":\"lie\",\"age\":\"18\",\"score\":\"99\"}]";
List<String> list=JSONObject.parseArray(json2,String.class);     //json字符串转Java数组,parseArray(json字符串,类.class)
List<Student> list1=JSONObject.parseArray(json2,Student.class);
System.out.println(JSONObject.toJSONString(list));    //数组转json字符串

注意:解析json字符串的类,必须有空参构造,必须是私有属性,必须有正确的get/set,重写toString

二.excel文件解析

1.读取文件

(1)加载excel

(2)调用poi创建excel对象

(3)获取指定的sheet

(4)获取指定的row

(5)获取指定的cell

(6)打印内容

(7)关流

for循环:

FileInputStream excel=new FileInputStream("src/test/resources/testcase.xlsx");    //加载excel
Workbook sheets=WorkbookFactory.create(excel);         //调用poi创建excel对象.factory为工厂类,创建对象
Sheet sheet = sheets.getSheet("register");            //获取指定的sheet
int lastRowNum = sheet.getLastRowNum();
for (int i=0;i<=lastRowNum;i++) {
    Row row = sheet.getRow(i);                          //获取指定的row
    int lastCellNum = row.getLastCellNum();
    for (int j = 0; j < lastCellNum; j++) {
        //枚举:防止别人传一些错误的参数
        Cell cell = row.getCell(j,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);            //获取指定的cell,加后面的遇到空格可跳过,不会出现null
        //遇到表格中的数字类型解决方法,1.强行转化为字符串类型,2.将获得的类型转为字符串,判断是否为字符串/字符串
        cell.setCellType(CellType.STRING);             //方法1.强行转换类型
        String cellValue=cell.getStringCellValue();
        System.out.println(cellValue);
        if (cell.getCellType().toString().equals("STRING")) {     //方法2.用if判断类型
            Object cellValue = cell.getStringCellValue();
            System.out.println(cellValue);                        //打印内容
        } else {
             Object cellValue = cell.getNumericCellValue();
             System.out.println(cellValue);                       //打印内容
        }
    }
}
excel.close;     //关流

增强for循环:通过增强for可去除excel第一行

FileInputStream excel=new FileInputStream("src/test/resources/testcase.xlsx");    //加载excel
Workbook sheets=WorkbookFactory.create(excel);         //调用poi创建excel对象.factory为工厂类,创建对象
Sheet sheet = sheets.getSheet("register");            //获取指定的sheet
int rownum=0;
for (Row row:sheet){                 //增强for
    if (rownum==0){
        rownum++;
        continue;
    }
    for (Cell cell:row){
        cell.setCellType(CellType.STRING);
        String cellValue=cell.getStringCellValue();
        System.out.println(cellValue);
    }
}

 2.写入

Row row=sheet.getRow(1);
Cell cell=row.getCell(1);
cell.setCellValue("scarlett");     //对单元格写入内容
FileOutputStream fos=new FileOutputStream("src/test/resources/testcase.xlsx");    //如果写的文件存在,会清空,如果写的文件不存在,会创建
//回写
sheets.write(fos);
excel.close();

三.properties文件解析

格式为HashMap

1.读取

Properties prop2=new Properties();
FileInputStream fis= null;
try {
    //try里面放入认为会有异常的代码
    fis = new FileInputStream("src/test/resources/config.properties");
    prop2.load(fis);
} catch (Exception e) {
    System.out.println("放入出现异常后的补偿操作,出现异常后直接跳到执行catch,无异常不执行");
    e.printStackTrace();
}finally {          //关流,释放资源
    System.out.println("不管是否出现异常都会执行的操作");
}
System.out.println(prop2);

2.写入

public static void main(String[] args) throws Exception{
        Properties prop=new Properties();
        prop.setProperty("username","scarlett");
        prop.setProperty("password","123456");
        prop.setProperty("phone","13039012776");
        FileOutputStream fos = new FileOutputStream("src/test/resources/config.properties");   //必须在main方法后添加throws Expection或使用try,否则报异常。
        prop.store(fos,"备注");

注意

1.异常处理:(1)在main方法后添加throws Expection抛出异常;(2)try...catch处理异常

posted @ 2025-04-21 17:22  思佳丽  阅读(37)  评论(0)    收藏  举报