73
73
import java .io .FileWriter ;
74
74
import java .io .IOException ;
75
75
import java .io .PrintWriter ;
76
+ import java .nio .file .Files ;
77
+ import java .nio .file .Paths ;
76
78
import java .util .ArrayList ;
77
79
import java .util .Collections ;
78
80
import java .util .regex .Pattern ;
@@ -97,37 +99,7 @@ public class CSVFile {
97
99
* @purpose loads the CSV-file and fills the inner table with the data
98
100
*/
99
101
public CSVFile (String path , char seperator ) {
100
- table = new ArrayList <ArrayList <String >>();
101
- trackList = new ArrayList <Integer >();
102
- pathCSVFile = path ;
103
- this .seperator = seperator ;
104
- String row = null ;
105
- File file = new File (path );
106
- ArrayList <String > colums = new ArrayList <String >();
107
- if (!file .canRead () || !file .isFile ()) {
108
- System .out .println ("unable to open file" );
109
- System .exit (1 );
110
- }
111
- BufferedReader in = null ;
112
- try {
113
- in = new BufferedReader (new FileReader (path ));
114
- while ((row = in .readLine ()) != null ) {
115
- // uses the compile method to compile the row
116
- // in its columns.
117
- table .add (compile (row , seperator ));
118
- }
119
- } catch (IOException e ) {
120
- e .printStackTrace ();
121
- } finally {
122
- if (in != null ) {
123
- try {
124
- in .close ();
125
- } catch (IOException e ) {
126
- e .printStackTrace ();
127
- }
128
- }
129
- }
130
-
102
+ this (new File (path ),seperator );
131
103
}
132
104
133
105
@@ -141,32 +113,17 @@ public CSVFile(File file, char seperator) {
141
113
trackList = new ArrayList <Integer >();
142
114
pathCSVFile = file .getPath ();
143
115
this .seperator = seperator ;
144
- String row = null ;
145
116
ArrayList <String > colums = new ArrayList <String >();
146
117
if (!file .canRead () || !file .isFile ()) {
147
118
System .out .println ("unable to open file" );
148
119
System .exit (1 );
149
120
}
150
- BufferedReader in = null ;
151
- try {
152
- in = new BufferedReader (new FileReader (file ));
153
- while ((row = in .readLine ()) != null ) {
154
- // uses the compile method to compile the row
155
- // in its columns.
156
- table .add (compile (row , seperator ));
157
- }
121
+
122
+ try (BufferedReader br = Files .newBufferedReader (Paths .get (file .getAbsolutePath ()))) {
123
+ br .lines ().forEach (line -> table .add (compile (line , seperator )));
158
124
} catch (IOException e ) {
159
125
e .printStackTrace ();
160
- } finally {
161
- if (in != null ) {
162
- try {
163
- in .close ();
164
- } catch (IOException e ) {
165
- e .printStackTrace ();
166
- }
167
- }
168
126
}
169
-
170
127
}
171
128
172
129
@@ -275,14 +232,14 @@ public static ArrayList<String> compile(String row, char sep) {
275
232
return columns ;
276
233
}
277
234
278
-
235
+ private static Pattern PATTERN_PUNCTUATION = Pattern . compile ( " \\ p{Punct}" );
279
236
/**
280
237
*
281
238
* @param ch
282
239
* @returns true if ch is punctuation character otherwise false.
283
240
*/
284
241
public static boolean isPunctuation (char ch ) {
285
- return Pattern . matches ( " \\ p{Punct}" , "" + ch );
242
+ return PATTERN_PUNCTUATION . matcher ( "" + ch ). matches ( );
286
243
}
287
244
288
245
@@ -489,18 +446,16 @@ public ArrayList<String> findRow(String key) {
489
446
* @returns true if a row contains 'key' otherwise false.
490
447
*/
491
448
public boolean contains (String key ) {
492
- boolean ans = false ;
493
449
key = key .trim ();
494
450
for (int i = 0 ; i < table .size (); i ++) {
495
451
for (String item : table .get (i )) {
496
452
item = item .trim ();
497
453
if (item .equals (key )) {
498
- ans = true ;
499
- break ;
454
+ return true ;
500
455
}
501
456
}
502
457
}
503
- return ans ;
458
+ return false ;
504
459
}
505
460
506
461
0 commit comments