JAVA 读取json、csv、txt、xls文件

读取json、csv、txt文件代码

/**
* 读取文件,json、csv、txt都通用
* fileName:文件路径
*/
public static String read( String fileName ) {
        StringBuffer sb = new StringBuffer();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "utf-8"));//GBK
            String line = null;
            while((line=reader.readLine())!=null){
                sb.append(line+"\n");
            }
            reader.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

测试

public void test() {
//        String fileName = "covidData/covid_4_5.txt";//txt文件
        String fileName = "covidData/worldhistory_4_5.csv";//csv文件
//        String fileName = "covidData/data.json";//json文件
        String path = Read.class.getClassLoader().getResource(fileName).getPath();//获取路径,这里我是放在配置文件夹下
        path = path.replaceAll("%20", " ");//无法识别空格的转义字符,如果路径有空格的话
        String fileData = Read.read(path);//读取文件
        System.out.println(fileData);//输出文件
    }

对json格式的操作:java的json操作


读取xls文件

导入对应的jar包
  
      net.sourceforge.jexcelapi
      jxl
      2.6.12
  
操作方法:
  • String path = “文件路径”;//文件路径
  • InputStream is = new FileInputStream(path);
  • Workbook wb = Workbook.getWorkbook(is);//读取文件
  • 获取页签的数量: int sheet_size = wb.getNumberOfSheets();
  • 每个页签创建一个Sheet对象:Sheet sheet = wb.getSheet(index);
  • 获取总行数: int rows = sheet.getRows();
  • 获取总列数:int columns = sheet.getColumns();
  • 获取表格中的数据 String cellinfo = sheet.getCell(col, row).getContents();
  • 获取页签名:String sheetName= sheet.getName();

例子:我这里是读取文件存入到数据库中
@org.junit.Test
    public void testXls() throws Exception {
        int cnt = 1;
        for ( int k = 1; k <= 4; k++ ) {
            String fileName = "covidData/temp"+k+".xls";//文件在类路径的位置
            String path = Read.class.getClassLoader().getResource(fileName).getPath();//获取路径,这里我是放在配置文件夹下
            path = path.replaceAll("%20", " ");//无法识别空格的转义字符,如果路径有空格的话

            InputStream is = new FileInputStream(path);
            Workbook wb = Workbook.getWorkbook(is);
            // Excel的页签数量
            int sheet_size = wb.getNumberOfSheets();
            for ( int index = 0; index < sheet_size; index++ ) {
                // 每个页签创建一个Sheet对象
                Sheet sheet = wb.getSheet(index);

                String className = sheet.getName();

                // sheet.getRows()返回该页的总行数
                out:
                for (int i = 3; i < sheet.getRows(); i++) {
                    Student student = new Student();
                    String provinceId = RandomCode.randomProvinceId(addressMapDao);
                    student.setProvinceId(provinceId);
                    student.setCityId(RandomCode.randomCityId(addressMapDao, provinceId.substring(0, 2)+"%"));
                    student.setClassId(classDao.findIdByName(className));
                    // sheet.getColumns()返回该页的总列数
                    for (int j = 1; j <= 2; j++) {
                        String cellinfo = sheet.getCell(j, i).getContents();
                        if ( cellinfo.equals("") ) {
                            continue out;
                        }

                        if ( j == 1 ) student.setStudentId(cellinfo);
                        if ( j == 2 ) student.setStudentName(cellinfo);

                    }
                    System.out.println(student);
//                    studentDao.saveStudent(student);
                }
            }

            System.out.println("\n\n\n");
        }

    }

你可能感兴趣的:(总结)