package com.iailab.module.model.mpk.common.utils; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.util.ArrayList; import java.util.List; /** * @Auther: Forrest * @Date: 2020/4/23 09:50 * @Description: */ public class Readtxt { public float[] read(String filePath, int filelength) { float data[] = new float[filelength]; try { File file = new File(filePath); if (file.isFile() && file.exists()) { InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8"); BufferedReader br = new BufferedReader(isr); for (int i = 0; i < filelength; i++) data[i] = Float.parseFloat(br.readLine()); br.close(); } else { System.out.print("文件不存在"); } } catch (Exception e) { System.out.println("文件读取错误"); } return data; } public float[] read1(String filePath, int filelength) { float data[] = new float[filelength]; try { File file = new File(filePath); if (file.isFile() && file.exists()) { InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8"); BufferedReader br = new BufferedReader(isr); br.readLine(); for (int i = 0; i < filelength; i++) data[i] = Float.parseFloat(br.readLine()); br.close(); } else { System.out.println("文件不存在"); } } catch (Exception e) { System.out.println("文件读取错误"); } return data; } public double[][] readCSV(String filePath, int row, int col) { double[][] res = new double[row][col]; String line = null; try { //BufferedReader bufferedReader=new BufferedReader(new FileReader(filePath)); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "utf-8")); int i = 0; while ((line = bufferedReader.readLine()) != null && i < row) { //数据行 String[] items = line.split(","); for (int j = 0; j < col; j++) { res[i][j] = Double.parseDouble(items[j]); } i++; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return res; } public String[][] readCSVString(String filePath, int row, int col) { String[][] res = new String[row][col]; String line = null; try { //BufferedReader bufferedReader=new BufferedReader(new FileReader(filePath)); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "utf-8")); int i = 0; while ((line = bufferedReader.readLine()) != null && i < row) { //数据行 String[] items = line.split("\t"); for (int j = 0; j < col; j++) { res[i][j] = items[j]; } i++; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return res; } public static List readMethodExcel(MultipartFile file) throws IOException { List datas; try (Workbook workbook = WorkbookFactory.create(file.getInputStream())) { //获取工作薄里面sheet的个数 int sheetNum = workbook.getNumberOfSheets(); datas = new ArrayList<>(sheetNum - 1); //从第二个sheet开始读,第一个为示例 for (int i = 1; i < sheetNum; i++) { Sheet sheet = workbook.getSheetAt(i); //获取sheet中有数据的行数 int rows = sheet.getPhysicalNumberOfRows(); double[][] doubles2 = new double[rows][]; for (int j = 0; j < rows; j++) { //获取每一行的数据 Row row = sheet.getRow(j); //得到每一行中有效单元格的数据 short cells = row.getLastCellNum(); //定义一个doubles数组用来存储读取单元格的数据 double[] doubles1 = new double[cells]; //遍历每一个有效的单元格数据 for (int k = 0; k < cells; k++) { //获取每一个单元格的数据 Cell cell = row.getCell(k, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); doubles1[k] = cell.getNumericCellValue(); } doubles2[j] = doubles1; } datas.add(doubles2); } } catch (Exception e) { throw new RuntimeException("解析Excel异常",e); } return datas; } }