CSV data manipulation: Difference between revisions

m (Fixed image link)
Line 2,312:
 
=={{header|Java}}==
<syntaxhighlight lang=java>
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
</syntaxhighlight>
<syntaxhighlight lang=java>
public class CSV {
public static void main(String[] args) throws IOException {
CSV csv = new CSV("data.csv");
csv.sumAllRows();
csv.write();
}
 
private final File file;
private List<List<String>> data;
 
public CSV(File file) throws IOException {
this.file = file;
open();
}
 
/* convenience constructor */
public CSV(String path) throws IOException {
this(new File(path));
}
 
public void sumAllRows() {
appendColumn("SUM");
int sum;
int length;
for (int index = 1; index < data.size(); index++) {
sum = sum(data.get(index));
length = data.get(index).size();
data.get(index).set(length - 1, String.valueOf(sum));
}
}
 
private int sum(List<String> row) {
int sum = 0;
for (int index = 0; index < row.size() - 1; index++)
sum += Integer.parseInt(row.get(index));
return sum;
}
 
private void appendColumn(String title) {
List<String> titles = data.get(0);
titles.add(title);
/* append an empty cell to each row */
for (int index = 1; index < data.size(); index++)
data.get(index).add("");
}
 
private void open() throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
data = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
/* using a limit of -1 will preserve trailing commas */
data.add(new ArrayList<>(List.of(line.split(",", -1))));
}
}
}
 
public void write() throws IOException {
try (FileWriter writer = new FileWriter(file)) {
String newline = System.lineSeparator();
for (List<String> row : data) {
writer.write(String.join(",", row));
writer.write(newline);
}
writer.flush();
}
}
}
</syntaxhighlight>
First iteration
<pre>
C1,C2,C3,C4,C5,SUM
1,5,9,13,17,45
2,6,10,14,18,50
3,7,11,15,19,55
4,8,12,16,20,60
</pre>
Second iteration
<pre>
C1,C2,C3,C4,C5,SUM,SUM
1,5,9,13,17,45,90
2,6,10,14,18,50,100
3,7,11,15,19,55,110
4,8,12,16,20,60,120
</pre>
<br />
===Roll Your Own===
<syntaxhighlight lang=java>import java.io.*;
118

edits