Skip to content

Commit c0b44d6

Browse files
committed
version 1
1 parent 8406650 commit c0b44d6

File tree

4 files changed

+83
-151
lines changed

4 files changed

+83
-151
lines changed

src/systemx/utils/CsvTools.java

Lines changed: 79 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88

99
import systemx.exceptions.DoNotExistsException;
1010

11-
12-
//FIXME: add exception throwing
13-
11+
//TODO: add the exception throw to the methods that can throw an exception (safe)
1412
/**
1513
* A utility class for working with CSV files
1614
*
1715
* @author Younes Rabeh
18-
* @version under development
16+
* @version 1.0
1917
*/
2018
public final class CsvTools {
2119
// 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() {}
3129
* @param rowData The data to append
3230
* @throws DoNotExistsException If the file does not exist
3331
*/
34-
public static void appendToCsvFile(File file, String[] rowData) throws DoNotExistsException {
32+
private static void appendToCsvFile(File file, String[] rowData) throws DoNotExistsException {
3533
FileManager.appendToFile(file, String.join(",", rowData));
3634
}
3735

@@ -41,7 +39,7 @@ public static void appendToCsvFile(File file, String[] rowData) throws DoNotExis
4139
* @return The data in the CSV file
4240
* @throws DoNotExistsException If the file does not exist
4341
*/
44-
public static List<String[]> getCsvFile(File file) throws DoNotExistsException {
42+
private static List<String[]> getCsvFile(File file) throws DoNotExistsException {
4543
List<String[]> csvData = new ArrayList<>();
4644
List<String> lines = FileManager.getFileLines(file);
4745
for (String line : lines) {
@@ -97,6 +95,16 @@ public static List<String[]> getRowsBelow(File file, Integer index) throws DoNot
9795
return csvData;
9896
}
9997

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+
100108
/**
101109
* Gets a column from a CSV file
102110
* @param file The CSV file
@@ -116,25 +124,6 @@ public static String[] getColumn(File file, Integer columnIndex) throws DoNotExi
116124
return columnData.toArray(new String[0]);
117125
}
118126

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-
138127
/**
139128
* Gets a column from a CSV file
140129
* @param file The CSV file
@@ -191,6 +180,24 @@ public static void appendFile(File file, File fileToAppend) throws DoNotExistsEx
191180
}
192181
}
193182

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+
194201
//TODO: pick-up the rows using the init primary key, [get throughout the record]
195202
/**
196203
* Inserts a row into a CSV file
@@ -232,37 +239,56 @@ public static void insertRows(File file, int index, List<String[]> rowsData) thr
232239
FileManager.overrideFile(file, lines.toArray(new String[0]));
233240
}
234241

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+
}
242255

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+
}
243271

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+
}
260288
}
261-
} catch (IndexOutOfBoundsException e) {
262-
System.out.println("Index out of bounds");
289+
rowString.deleteCharAt(rowString.length() - 1);
290+
lines.set(i, rowString.toString());
263291
}
292+
FileManager.overrideFile(file, lines.toArray(new String[0]));
264293
}
265-
266-
267-
268294
}

src/systemx/utils/FileManager.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package systemx.utils;
22

3-
import java.io.*;
4-
import java.util.Arrays;
53
import java.util.List;
6-
4+
import java.io.*;
75
import java.util.ArrayList;
86

97
import systemx.exceptions.DoNotExistsException;
@@ -15,7 +13,7 @@
1513
* This class provides methods to work with an individual file.
1614
*
1715
* @author Younes Rabeh
18-
* @version under development
16+
* @version 1.0
1917
*/
2018
public final class FileManager {
2119
private FileManager(){}
@@ -383,7 +381,7 @@ public static void deleteLine(File file, Integer lineNumber) throws DoNotExistsE
383381
overrideFile(file, lines.toArray(new String[0]));
384382
}
385383

386-
static boolean lineCheck(File file) {
384+
private static boolean lineCheck(File file) {
387385
try {
388386
RandomAccessFile raf = new RandomAccessFile(file, "rw");
389387
long length = raf.length();
@@ -400,4 +398,4 @@ static boolean lineCheck(File file) {
400398
return true; // Return false if an IOException occurred
401399
}
402400
}
403-
}
401+
}

src/tests/Test1.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

test.csv

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)