Skip to content

Commit 8406650

Browse files
committed
column append update
1 parent b37f21a commit 8406650

File tree

1 file changed

+268
-0
lines changed

1 file changed

+268
-0
lines changed

src/systemx/utils/CsvTools.java

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
package systemx.utils;
2+
3+
import java.util.List;
4+
5+
import java.util.Arrays;
6+
import java.io.File;
7+
import java.util.ArrayList;
8+
9+
import systemx.exceptions.DoNotExistsException;
10+
11+
12+
//FIXME: add exception throwing
13+
14+
/**
15+
* A utility class for working with CSV files
16+
*
17+
* @author Younes Rabeh
18+
* @version under development
19+
*/
20+
public final class CsvTools {
21+
// The file must be in the format of a CSV file, the first line is the header, and the rest are the data
22+
// Prevent instantiation
23+
private CsvTools() {}
24+
25+
26+
//TODO: add the access modifier to specific methods
27+
28+
/**
29+
* Appends a row of data to a CSV file
30+
* @param file The CSV file
31+
* @param rowData The data to append
32+
* @throws DoNotExistsException If the file does not exist
33+
*/
34+
public static void appendToCsvFile(File file, String[] rowData) throws DoNotExistsException {
35+
FileManager.appendToFile(file, String.join(",", rowData));
36+
}
37+
38+
/**
39+
* Reads a CSV file and returns the data as a list of arrays
40+
* @param file The CSV file
41+
* @return The data in the CSV file
42+
* @throws DoNotExistsException If the file does not exist
43+
*/
44+
public static List<String[]> getCsvFile(File file) throws DoNotExistsException {
45+
List<String[]> csvData = new ArrayList<>();
46+
List<String> lines = FileManager.getFileLines(file);
47+
for (String line : lines) {
48+
csvData.add(line.split(","));
49+
}
50+
return csvData;
51+
}
52+
53+
/**
54+
* Reads a CSV file and returns a row as a list of arrays
55+
* @param file The path to the CSV file
56+
* @param index The index of the row to get
57+
* @return The row in the CSV file
58+
* @throws DoNotExistsException If the file does not exist
59+
*/
60+
public static String[] getRow(File file, Integer index) throws DoNotExistsException {
61+
String fileContent = FileManager.getFileLine(file, index);
62+
if (fileContent != null) {
63+
return fileContent.split(",");
64+
}
65+
return null; // Index out of bounds
66+
}
67+
68+
/**
69+
* Gets all the rows below a certain index in a CSV file
70+
* @param file The CSV file
71+
* @param index The index of the row to get all the rows below
72+
* @return The rows above the index in the CSV file
73+
* @throws DoNotExistsException If the file does not exist
74+
*/
75+
public static List<String[]> getRowsAbove(File file, Integer index) throws DoNotExistsException {
76+
List<String[]> csvData = new ArrayList<>();
77+
List<String> lines = FileManager.getLinesAbove(file, index);
78+
for (String line : lines) {
79+
csvData.add(line.split(","));
80+
}
81+
return csvData;
82+
}
83+
84+
/**
85+
* Gets all the rows below a certain index in a CSV file
86+
* @param file The CSV file
87+
* @param index The index of the row to get all the rows below
88+
* @return The rows below the index in the CSV file
89+
* @throws DoNotExistsException If the file does not exist
90+
*/
91+
public static List<String[]> getRowsBelow(File file, Integer index) throws DoNotExistsException {
92+
List<String[]> csvData = new ArrayList<>();
93+
List<String> lines = FileManager.getLinesBelow(file, index);
94+
for (String line : lines) {
95+
csvData.add(line.split(","));
96+
}
97+
return csvData;
98+
}
99+
100+
/**
101+
* Gets a column from a CSV file
102+
* @param file The CSV file
103+
* @param columnIndex The index of the column to get
104+
* @return The column data
105+
* @throws DoNotExistsException If the file does not exist
106+
*/
107+
public static String[] getColumn(File file, Integer columnIndex) throws DoNotExistsException {
108+
List<String[]> csvData = getCsvFile(file);
109+
List<String> columnData = new ArrayList<>();
110+
111+
for (String[] row : csvData) {
112+
if (row.length > columnIndex) {
113+
columnData.add(row[columnIndex]);
114+
}
115+
}
116+
return columnData.toArray(new String[0]);
117+
}
118+
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+
/**
139+
* Gets a column from a CSV file
140+
* @param file The CSV file
141+
* @param columnIndices The indices of the columns to get
142+
* @return The columns data
143+
* @throws DoNotExistsException If the file does not exist
144+
*/
145+
public static List<String[]> getColumns(File file, Integer... columnIndices) throws DoNotExistsException {
146+
List<String[]> columnsData = new ArrayList<>();
147+
List<String[]> csvData = getCsvFile(file);
148+
for (int columnIndex : columnIndices) {
149+
List<String> columnData = new ArrayList<>();
150+
for (String[] row : csvData) {
151+
if (row.length > columnIndex) {
152+
columnData.add(row[columnIndex]);
153+
}
154+
}
155+
columnsData.add(columnData.toArray(new String[0]));
156+
}
157+
return columnsData;
158+
}
159+
160+
/**
161+
* Adds a row to a CSV file
162+
* @param file The CSV file
163+
* @param rowData The data to add
164+
* @throws DoNotExistsException If the file does not exist
165+
*/
166+
public static void appendRow(File file, String[] rowData) throws DoNotExistsException {
167+
appendToCsvFile(file, rowData);
168+
}
169+
170+
/**
171+
* Adds multiple rows to a CSV file
172+
* @param file The CSV file
173+
* @param rowsData The data to add
174+
* @throws DoNotExistsException If the file does not exist
175+
*/
176+
public static void appendRows(File file, List<String[]> rowsData) throws DoNotExistsException {
177+
for (String[] row : rowsData) {
178+
appendToCsvFile(file, row);
179+
}
180+
}
181+
182+
/**
183+
* Appends the content of a CSV file to another CSV file
184+
* @param file The file to append to
185+
* @param fileToAppend The file to append
186+
* @throws DoNotExistsException If the file does not exist
187+
*/
188+
public static void appendFile(File file, File fileToAppend) throws DoNotExistsException {
189+
for (String[] row : getCsvFile(fileToAppend)) {
190+
appendToCsvFile(file, row);
191+
}
192+
}
193+
194+
//TODO: pick-up the rows using the init primary key, [get throughout the record]
195+
/**
196+
* Inserts a row into a CSV file
197+
* @param file The CSV file
198+
* @param index The index to insert the row at
199+
* @param rowData The data to insert
200+
* @throws DoNotExistsException If the file does not exist
201+
*/
202+
public static void insertRow(File file, int index, String[] rowData) throws DoNotExistsException {
203+
List<String> lines = new ArrayList<>(FileManager.getFileLines(file));
204+
if (index < 0 || index > lines.size()) throw new IndexOutOfBoundsException();
205+
StringBuilder rowString = new StringBuilder();
206+
for (String data : rowData) {
207+
rowString.append(data).append(",");
208+
}
209+
rowString.deleteCharAt(rowString.length() - 1);
210+
lines.add(index, rowString.toString());
211+
FileManager.overrideFile(file, lines.toArray(new String[0]));
212+
}
213+
214+
/**
215+
* Inserts multiple rows into a CSV file
216+
* @param file The CSV file
217+
* @param index The index to insert the rows at
218+
* @param rowsData The data to insert
219+
* @throws DoNotExistsException If the file does not exist
220+
* @throws IndexOutOfBoundsException If the index is out of bounds
221+
*/
222+
public static void insertRows(File file, int index, List<String[]> rowsData) throws DoNotExistsException {
223+
List<String> lines = new ArrayList<>(FileManager.getFileLines(file));
224+
final int size = FileManager.countLines(file);
225+
if (index < 0 || index > lines.size()) throw new IndexOutOfBoundsException();
226+
StringBuilder rowString = new StringBuilder();
227+
for (String[] data : rowsData) {
228+
rowString.append(Arrays.toString(data)).append("\n");
229+
}
230+
rowString.deleteCharAt(rowString.length() - 1);
231+
lines.add(index, rowString.toString());
232+
FileManager.overrideFile(file, lines.toArray(new String[0]));
233+
}
234+
235+
//TODO: add column insertion
236+
237+
238+
239+
240+
241+
242+
243+
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());
260+
}
261+
} catch (IndexOutOfBoundsException e) {
262+
System.out.println("Index out of bounds");
263+
}
264+
}
265+
266+
267+
268+
}

0 commit comments

Comments
 (0)