7373import java .io .FileWriter ;
7474import java .io .IOException ;
7575import java .io .PrintWriter ;
76+ import java .nio .file .Files ;
77+ import java .nio .file .Paths ;
7678import java .util .ArrayList ;
7779import java .util .Collections ;
7880import java .util .regex .Pattern ;
@@ -97,37 +99,7 @@ public class CSVFile {
9799 * @purpose loads the CSV-file and fills the inner table with the data
98100 */
99101 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 );
131103 }
132104
133105
@@ -141,32 +113,17 @@ public CSVFile(File file, char seperator) {
141113 trackList = new ArrayList <Integer >();
142114 pathCSVFile = file .getPath ();
143115 this .seperator = seperator ;
144- String row = null ;
145116 ArrayList <String > colums = new ArrayList <String >();
146117 if (!file .canRead () || !file .isFile ()) {
147118 System .out .println ("unable to open file" );
148119 System .exit (1 );
149120 }
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 )));
158124 } catch (IOException e ) {
159125 e .printStackTrace ();
160- } finally {
161- if (in != null ) {
162- try {
163- in .close ();
164- } catch (IOException e ) {
165- e .printStackTrace ();
166- }
167- }
168126 }
169-
170127 }
171128
172129
@@ -275,14 +232,14 @@ public static ArrayList<String> compile(String row, char sep) {
275232 return columns ;
276233 }
277234
278-
235+ private static Pattern PATTERN_PUNCTUATION = Pattern . compile ( " \\ p{Punct}" );
279236 /**
280237 *
281238 * @param ch
282239 * @returns true if ch is punctuation character otherwise false.
283240 */
284241 public static boolean isPunctuation (char ch ) {
285- return Pattern . matches ( " \\ p{Punct}" , "" + ch );
242+ return PATTERN_PUNCTUATION . matcher ( "" + ch ). matches ( );
286243 }
287244
288245
@@ -489,18 +446,16 @@ public ArrayList<String> findRow(String key) {
489446 * @returns true if a row contains 'key' otherwise false.
490447 */
491448 public boolean contains (String key ) {
492- boolean ans = false ;
493449 key = key .trim ();
494450 for (int i = 0 ; i < table .size (); i ++) {
495451 for (String item : table .get (i )) {
496452 item = item .trim ();
497453 if (item .equals (key )) {
498- ans = true ;
499- break ;
454+ return true ;
500455 }
501456 }
502457 }
503- return ans ;
458+ return false ;
504459 }
505460
506461
0 commit comments