Java读取txt文件、excel文件的方法

Java读取txt文件、excel文件的方法

1.读取txt文件

 public static String getFileContent(String filePath,String charset){//filePath为文件路径,charset为字符编码。通常使用UTF-8
		File file = new File(filePath);
	    BufferedReader reader = null;
	    String tempString = null;
	    int line =1;
	    StringBuffer str = new StringBuffer();
	    try {
	        log.error("以行为单位读取文件内容,一次读一整行:");
	        InputStreamReader isr = new InputStreamReader(new FileInputStream(file), charset);  
	        reader = new BufferedReader(isr);
	        while ((tempString = reader.readLine()) != null) {
	        	//log.info("Line"+ line + ":" +tempString);
	        	str.append(tempString + "\r\n");
	            line ++ ;
	        }
	        reader.close();
	    } catch (FileNotFoundException e) {
	    	log.error("文件不存在:",e);
	    } catch (IOException e) {
	        // TODO Auto-generated catch block
	    	log.error("文件读取异常:",e);
	    }finally{
	        if(reader != null){
	            try {
	                reader.close();
	            } catch (IOException e) {
	            	log.error("文件读取异常:",e);
	            }
	        }
	    }
	    System.out.println(str.toString());
	    return str.toString();
	}

读取的数据为一行一行的字符串,可以使用String接收,使用split切分,获取自己需要的数据,举例如下:

String response = FileHelper.getFileContent(fileUrl,"UTF-8");
            if(!StringUtil.isEmptyStr(response)){
                String[] sz = response.split("\\n");
                Map<String,Object> statementMap = null;
                List<Map<String,Object>> statementList = new ArrayList<Map<String,Object>>();
                for (int i = 0; i < sz.length; i++){
                        statementMap = new HashMap<String,Object>();
                        String[] statement = sz[i].split("\\|\\|\\|",-1);
                        String account = statement[0];
                        String figer = statement[1];
                        String number = statement[2];
                        String proName = statement[3];
                        String expTime = statement[4];

                        statementMap.put("account", account);
                        statementMap.put("figer", figer);
                        statementMap.put("number", number);
                        statementMap.put("proName", proName);
                        statementMap.put("expTime", expTime);
                        statementList.add(statementMap);
                }
                resultMap.put("list", statementList);

2.读取excel文件

   /*
	* 获取excel某列数据方法
	*/
    public static List<String> readExcel(String filePath){
        File file = new File(filePath);
        List<String> headList = new ArrayList<>();
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            XSSFWorkbook wb = new XSSFWorkbook(in);
            XSSFSheet sheet = wb.getSheetAt(0); //获取第一张工作表
            int firstRowNum = sheet.getFirstRowNum();
            int lastRowNum = sheet.getLastRowNum();
            XSSFRow row = sheet.getRow(sheet.getFirstRowNum());//判断表行是否为空
            if (null == row){//表空,直接返回
                return headList;
            }
            for (int i = firstRowNum + 1; i <= lastRowNum; i++) {//遍历行,第一行为表头,跳过
                row = sheet.getRow(i);
                XSSFCell cell = row.getCell(0);//取每行的第一列数据,可根据需要修改或者遍历
                if (StrHelper.isNotEmpty(cell.getStringCellValue().trim())){
                    headList.add(cell.getStringCellValue().trim());
                    System.out.println(cell.getStringCellValue().trim());
                }
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                if(in != null){
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return headList;
    }

读取的数据为String数组,可以使用List接收,获取自己需要的数据,举例如下:

List<String> response = FileHelper.readExcel(fileUrl);
posted @ 2021-07-18 22:26  初出茅庐小菜鸡  阅读(1330)  评论(0编辑  收藏  举报