提交 | 用户 | 时间
|
449017
|
1 |
package com.iailab.module.model.mpk.common.utils; |
D |
2 |
|
|
3 |
|
|
4 |
import org.apache.poi.ss.usermodel.*; |
|
5 |
import org.apache.poi.ss.util.CellRangeAddress; |
|
6 |
import org.springframework.web.multipart.MultipartFile; |
|
7 |
|
|
8 |
import java.io.*; |
|
9 |
import java.util.ArrayList; |
|
10 |
import java.util.List; |
|
11 |
|
|
12 |
/** |
|
13 |
* @Auther: Forrest |
|
14 |
* @Date: 2020/4/23 09:50 |
|
15 |
* @Description: |
|
16 |
*/ |
|
17 |
public class Readtxt { |
|
18 |
public float[] read(String filePath, int filelength) { |
|
19 |
float data[] = new float[filelength]; |
|
20 |
try { |
|
21 |
File file = new File(filePath); |
|
22 |
if (file.isFile() && file.exists()) { |
|
23 |
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8"); |
|
24 |
BufferedReader br = new BufferedReader(isr); |
|
25 |
for (int i = 0; i < filelength; i++) |
|
26 |
data[i] = Float.parseFloat(br.readLine()); |
|
27 |
br.close(); |
|
28 |
} else { |
|
29 |
System.out.print("文件不存在"); |
|
30 |
} |
|
31 |
} catch (Exception e) { |
|
32 |
System.out.println("文件读取错误"); |
|
33 |
} |
|
34 |
return data; |
|
35 |
|
|
36 |
} |
|
37 |
|
|
38 |
public float[] read1(String filePath, int filelength) { |
|
39 |
float data[] = new float[filelength]; |
|
40 |
try { |
|
41 |
File file = new File(filePath); |
|
42 |
if (file.isFile() && file.exists()) { |
|
43 |
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8"); |
|
44 |
BufferedReader br = new BufferedReader(isr); |
|
45 |
br.readLine(); |
|
46 |
for (int i = 0; i < filelength; i++) |
|
47 |
data[i] = Float.parseFloat(br.readLine()); |
|
48 |
br.close(); |
|
49 |
} else { |
|
50 |
System.out.println("文件不存在"); |
|
51 |
} |
|
52 |
} catch (Exception e) { |
|
53 |
System.out.println("文件读取错误"); |
|
54 |
} |
|
55 |
return data; |
|
56 |
|
|
57 |
} |
|
58 |
|
|
59 |
public double[][] readCSV(String filePath, int row, int col) { |
|
60 |
double[][] res = new double[row][col]; |
|
61 |
String line = null; |
|
62 |
try { |
|
63 |
//BufferedReader bufferedReader=new BufferedReader(new FileReader(filePath)); |
|
64 |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "utf-8")); |
|
65 |
|
|
66 |
int i = 0; |
|
67 |
while ((line = bufferedReader.readLine()) != null && i < row) { |
|
68 |
//数据行 |
|
69 |
String[] items = line.split(","); |
|
70 |
for (int j = 0; j < col; j++) { |
|
71 |
res[i][j] = Double.parseDouble(items[j]); |
|
72 |
} |
|
73 |
i++; |
|
74 |
} |
|
75 |
} catch (FileNotFoundException e) { |
|
76 |
e.printStackTrace(); |
|
77 |
} catch (IOException e) { |
|
78 |
e.printStackTrace(); |
|
79 |
} |
|
80 |
|
|
81 |
|
|
82 |
return res; |
|
83 |
} |
|
84 |
|
|
85 |
|
|
86 |
public String[][] readCSVString(String filePath, int row, int col) { |
|
87 |
String[][] res = new String[row][col]; |
|
88 |
String line = null; |
|
89 |
try { |
|
90 |
//BufferedReader bufferedReader=new BufferedReader(new FileReader(filePath)); |
|
91 |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "utf-8")); |
|
92 |
|
|
93 |
int i = 0; |
|
94 |
while ((line = bufferedReader.readLine()) != null && i < row) { |
|
95 |
//数据行 |
|
96 |
String[] items = line.split("\t"); |
|
97 |
for (int j = 0; j < col; j++) { |
|
98 |
res[i][j] = items[j]; |
|
99 |
} |
|
100 |
i++; |
|
101 |
} |
|
102 |
} catch (FileNotFoundException e) { |
|
103 |
e.printStackTrace(); |
|
104 |
} catch (IOException e) { |
|
105 |
e.printStackTrace(); |
|
106 |
} |
|
107 |
|
|
108 |
|
|
109 |
return res; |
|
110 |
} |
|
111 |
|
|
112 |
public static List<double[][]> readMethodExcel(MultipartFile file) throws IOException { |
912a1e
|
113 |
List<double[][]> datas; |
D |
114 |
try (Workbook workbook = WorkbookFactory.create(file.getInputStream())) { |
|
115 |
//获取工作薄里面sheet的个数 |
|
116 |
int sheetNum = workbook.getNumberOfSheets(); |
|
117 |
datas = new ArrayList<>(sheetNum - 1); |
|
118 |
//从第二个sheet开始读,第一个为示例 |
|
119 |
for (int i = 1; i < sheetNum; i++) { |
|
120 |
Sheet sheet = workbook.getSheetAt(i); |
|
121 |
//获取sheet中有数据的行数 |
|
122 |
int rows = sheet.getPhysicalNumberOfRows(); |
|
123 |
double[][] doubles2 = new double[rows][]; |
|
124 |
for (int j = 0; j < rows; j++) { |
|
125 |
//获取每一行的数据 |
|
126 |
Row row = sheet.getRow(j); |
|
127 |
//得到每一行中有效单元格的数据 |
|
128 |
short cells = row.getLastCellNum(); |
|
129 |
//定义一个doubles数组用来存储读取单元格的数据 |
|
130 |
double[] doubles1 = new double[cells]; |
|
131 |
//遍历每一个有效的单元格数据 |
|
132 |
for (int k = 0; k < cells; k++) { |
|
133 |
//获取每一个单元格的数据 |
|
134 |
Cell cell = row.getCell(k, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); |
|
135 |
doubles1[k] = cell.getNumericCellValue(); |
|
136 |
} |
|
137 |
doubles2[j] = doubles1; |
449017
|
138 |
} |
912a1e
|
139 |
datas.add(doubles2); |
449017
|
140 |
} |
912a1e
|
141 |
} catch (Exception e) { |
D |
142 |
throw new RuntimeException("解析Excel异常",e); |
449017
|
143 |
} |
D |
144 |
|
|
145 |
return datas; |
|
146 |
} |
|
147 |
} |