8
8
9
9
import systemx .exceptions .DoNotExistsException ;
10
10
11
-
12
- //FIXME: add exception throwing
13
-
11
+ //TODO: add the exception throw to the methods that can throw an exception (safe)
14
12
/**
15
13
* A utility class for working with CSV files
16
14
*
17
15
* @author Younes Rabeh
18
- * @version under development
16
+ * @version 1.0
19
17
*/
20
18
public final class CsvTools {
21
19
// The file must be in the format of a CSV file, the first line is the header, and the rest are the data
@@ -31,7 +29,7 @@ private CsvTools() {}
31
29
* @param rowData The data to append
32
30
* @throws DoNotExistsException If the file does not exist
33
31
*/
34
- public static void appendToCsvFile (File file , String [] rowData ) throws DoNotExistsException {
32
+ private static void appendToCsvFile (File file , String [] rowData ) throws DoNotExistsException {
35
33
FileManager .appendToFile (file , String .join ("," , rowData ));
36
34
}
37
35
@@ -41,7 +39,7 @@ public static void appendToCsvFile(File file, String[] rowData) throws DoNotExis
41
39
* @return The data in the CSV file
42
40
* @throws DoNotExistsException If the file does not exist
43
41
*/
44
- public static List <String []> getCsvFile (File file ) throws DoNotExistsException {
42
+ private static List <String []> getCsvFile (File file ) throws DoNotExistsException {
45
43
List <String []> csvData = new ArrayList <>();
46
44
List <String > lines = FileManager .getFileLines (file );
47
45
for (String line : lines ) {
@@ -97,6 +95,16 @@ public static List<String[]> getRowsBelow(File file, Integer index) throws DoNot
97
95
return csvData ;
98
96
}
99
97
98
+ /**
99
+ * Gets the Titles of the columns in a CSV file
100
+ * @param file The CSV file
101
+ * @return The titles of the columns
102
+ * @throws DoNotExistsException If the file does not exist
103
+ */
104
+ public static String [] getColumnsTitles (File file ) throws DoNotExistsException {
105
+ return getRow (file , 0 );
106
+ }
107
+
100
108
/**
101
109
* Gets a column from a CSV file
102
110
* @param file The CSV file
@@ -116,25 +124,6 @@ public static String[] getColumn(File file, Integer columnIndex) throws DoNotExi
116
124
return columnData .toArray (new String [0 ]);
117
125
}
118
126
119
- /**
120
- * Appends a column to a CSV file. Best effort. If the column data is less than the number of rows,
121
- * the column will be appended to the rows that have data.
122
- * @param file The CSV file
123
- * @param columnData The data to append
124
- * @throws DoNotExistsException If the file does not exist
125
- */
126
- public static void appendColumn (File file , String [] columnData ) throws DoNotExistsException {
127
- List <String > lines = new ArrayList <>(FileManager .getFileLines (file ));
128
- final int size = columnData .length ;
129
- for (int i = 0 ; i < lines .size (); i ++) {
130
- if (i < size ){
131
- lines .set (i , lines .get (i ) + "," + columnData [i ]);
132
- }
133
-
134
- }
135
- FileManager .overrideFile (file , lines .toArray (new String [0 ]));
136
- }
137
-
138
127
/**
139
128
* Gets a column from a CSV file
140
129
* @param file The CSV file
@@ -191,6 +180,24 @@ public static void appendFile(File file, File fileToAppend) throws DoNotExistsEx
191
180
}
192
181
}
193
182
183
+ /**
184
+ * Appends a column to a CSV file. Best effort. If the column data is less than the number of rows,
185
+ * the column will be appended to the rows that have data.
186
+ * @param file The CSV file
187
+ * @param columnData The data to append
188
+ * @throws DoNotExistsException If the file does not exist
189
+ */
190
+ public static void appendColumn (File file , String [] columnData ) throws DoNotExistsException {
191
+ List <String > lines = new ArrayList <>(FileManager .getFileLines (file ));
192
+ final int size = columnData .length ;
193
+ for (int i = 0 ; i < lines .size (); i ++) {
194
+ if (i < size ){
195
+ lines .set (i , lines .get (i ) + "," + columnData [i ]);
196
+ }
197
+ }
198
+ FileManager .overrideFile (file , lines .toArray (new String [0 ]));
199
+ }
200
+
194
201
//TODO: pick-up the rows using the init primary key, [get throughout the record]
195
202
/**
196
203
* Inserts a row into a CSV file
@@ -232,37 +239,56 @@ public static void insertRows(File file, int index, List<String[]> rowsData) thr
232
239
FileManager .overrideFile (file , lines .toArray (new String [0 ]));
233
240
}
234
241
235
- //TODO: add column insertion
236
-
237
-
238
-
239
-
240
-
241
-
242
+ /**
243
+ * Deletes a row from a CSV file
244
+ * @param file The CSV file
245
+ * @param index The index of the row to delete
246
+ * @throws DoNotExistsException If the file does not exist
247
+ * @throws IndexOutOfBoundsException If the index is out of bounds
248
+ */
249
+ public static void deleteRow (File file , int index ) throws DoNotExistsException {
250
+ List <String > lines = new ArrayList <>(FileManager .getFileLines (file ));
251
+ if (index < 0 || index >= lines .size ()) throw new IndexOutOfBoundsException ();
252
+ lines .remove (index );
253
+ FileManager .overrideFile (file , lines .toArray (new String [0 ]));
254
+ }
242
255
256
+ /**
257
+ * Deletes multiple rows from a CSV file
258
+ * @param file The CSV file
259
+ * @param integers The indices of the rows to delete
260
+ * @throws DoNotExistsException If the file does not exist
261
+ * @throws IndexOutOfBoundsException If the index is out of bounds
262
+ */
263
+ public static void deleteRows (File file , Integer ... integers ) throws DoNotExistsException {
264
+ List <String > lines = new ArrayList <>(FileManager .getFileLines (file ));
265
+ for (int index : integers ) {
266
+ if (index < 0 || index >= lines .size ()) throw new IndexOutOfBoundsException ();
267
+ lines .remove (index );
268
+ }
269
+ FileManager .overrideFile (file , lines .toArray (new String [0 ]));
270
+ }
243
271
244
- public static void main ( String [] args ) {
245
- try {
246
- File file = new File ( "test.csv" ); // replace with your file path
247
- String [] rowData = { "data1" , "data2" , "data5" , "jvc" , "SCS" , "L ADJC" }; // replace with your data
248
- int index = 1 ; // replace with your desired index
249
- try {
250
- //List<String[]> csvData = getColumns(file, 1,2);
251
- String [] data = { "sc" , "sc" , "sc" } ;
252
- String [] data1 = { "sc1" , "sc1" , "sc1" };
253
- List < String []> get = Arrays . asList ( data , data1 );
254
- //insertRows(file, 0, get );
255
- appendColumn ( file , rowData );
256
- List < String []> row = getRowsBelow ( file , 1 );
257
- System . out . println ( Arrays . deepToString ( row . toArray ()));
258
- } catch ( DoNotExistsException e ) {
259
- System . out . println ( e . getMessage ());
272
+ /**
273
+ * Deletes a column from a CSV file
274
+ * @param file The CSV file
275
+ * @param index The index of the column to delete
276
+ * @throws DoNotExistsException If the file does not exist
277
+ */
278
+ public static void deleteColumn ( File file , int index ) throws DoNotExistsException {
279
+ List < String > lines = new ArrayList <>( FileManager . getFileLines ( file )) ;
280
+ for ( int i = 0 ; i < lines . size (); i ++) {
281
+ String [] row = lines . get ( i ). split ( "," );
282
+ if ( index < 0 || index >= row . length ) throw new IndexOutOfBoundsException ( );
283
+ StringBuilder rowString = new StringBuilder ( );
284
+ for ( int j = 0 ; j < row . length ; j ++) {
285
+ if ( j != index ) {
286
+ rowString . append ( row [ j ]). append ( "," );
287
+ }
260
288
}
261
- } catch ( IndexOutOfBoundsException e ) {
262
- System . out . println ( "Index out of bounds" );
289
+ rowString . deleteCharAt ( rowString . length () - 1 );
290
+ lines . set ( i , rowString . toString () );
263
291
}
292
+ FileManager .overrideFile (file , lines .toArray (new String [0 ]));
264
293
}
265
-
266
-
267
-
268
294
}
0 commit comments