1
1
package javaToolkit .lib .utils ;
2
2
3
+ import java .io .BufferedReader ;
4
+ import java .io .FileReader ;
3
5
import java .io .FileWriter ;
6
+ import java .io .IOException ;
4
7
import java .nio .file .Path ;
5
8
import java .util .ArrayList ;
9
+ import java .util .Arrays ;
6
10
import java .util .List ;
7
11
import java .util .stream .Collectors ;
8
12
@@ -16,7 +20,7 @@ public static void writeLine2Csv(List<String> strList, Path filePath, Boolean if
16
20
if (ifAppendNewLine ) {
17
21
collect += "\n " ;
18
22
}
19
- System .out .println (collect );
23
+ // System.out.println(collect);
20
24
21
25
writer .write (collect );
22
26
writer .close ();
@@ -177,4 +181,34 @@ public static List<String> parseLine(String cvsLine, char separator, char quote)
177
181
return result ;
178
182
}
179
183
184
+ public static List <List <String >> loadCSVTo2DList (Path filePath , Boolean ifWithHeader ) {
185
+ String line = "" ;
186
+ String cvsSplitBy = "," ;
187
+ List <List <String >> data = new ArrayList <>();
188
+
189
+ try (BufferedReader br = new BufferedReader (new FileReader (filePath .toString ()))) {
190
+
191
+ // skip header
192
+ line = br .readLine ();
193
+ while ((line = br .readLine ()) != null ) {
194
+
195
+ // use comma as separator
196
+ String [] row = line .split (cvsSplitBy );
197
+ List <String > strings = Arrays .asList (row );
198
+ data .add (strings );
199
+
200
+ }
201
+
202
+ } catch (IOException e ) {
203
+ System .out .println (line );
204
+ e .printStackTrace ();
205
+ System .exit (0 );
206
+ } catch (ArrayIndexOutOfBoundsException e ) {
207
+ System .out .println (line );
208
+ e .printStackTrace ();
209
+ System .exit (0 );
210
+ }
211
+ return data ;
212
+ }
213
+
180
214
}
0 commit comments