潘志宝
2024-09-30 303715d81f0c1cd8b32cd659b7734c01565369a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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<double[][]> readMethodExcel(MultipartFile file) throws IOException {
        List<double[][]> 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;
    }
}