Repository navigation
Expand file tree
/
Copy pathCSVHandler.java
More file actions
189 lines (165 loc) · 6.82 KB
/
Copy pathCSVHandler.java
File metadata and controls
189 lines (165 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package handler;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.Validate;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
public class CSVHandler {
private File file;
private CSVWriteHandler writer;
public CSVHandler(String path, boolean deleteOld) throws Exception {
this.file = new File(path);
Validate.isTrue(!file.isDirectory(), "Specified path is a directory.");
// Check whether path is valid.
if (!file.exists()) {
try {
file.createNewFile();
file.delete();
} catch (Exception e) {
throw new Exception("File not creatable.");
}
} else if (deleteOld) {
file.delete();
file.createNewFile();
}
}
public class CSVWriteHandler {
private CSVWriter writer;
public CSVWriteHandler(String path, List<String> header) throws IOException {
this.writer = this.getNewCSVWriter(path);
writeLine(header);
}
public void writeLine(List<String> lineElements) throws IOException {
String[] lineArray = lineElements.toArray(new String[lineElements.size()]);
this.writer.writeNext(lineArray);
this.writer.flush();
}
public CSVWriter getNewCSVWriter(String path) throws IOException {
Writer BufWriter = Files.newBufferedWriter(Paths.get(path), StandardOpenOption.CREATE);
return new CSVWriter(BufWriter,
CSVWriter.DEFAULT_SEPARATOR,
CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.DEFAULT_ESCAPE_CHARACTER,
CSVWriter.DEFAULT_LINE_END);
}
public void close() throws IOException {
this.writer.close();
}
}
public void appendLeft(String header, List<String> data){
this.append(header, data, true);
}
public void appendRight(String header, List<String> data){
this.append(header, data, false);
}
private void append(String header, List<String> data, boolean appendLeft) {
List<String> resultLines = new LinkedList<>();
try {
BufferedReader br = new BufferedReader(new FileReader(file.getPath()));
String line = br.readLine();
if (appendLeft)
resultLines.add(header + "," + line);
else
resultLines.add(line + "," + header);
for (int i = 0; (line = br.readLine()) != null; i++) {
if (appendLeft)
resultLines.add(data.get(i) + "," + line);
else
resultLines.add(line + "," + data.get(i));
}
br.close();
Validate.isTrue(
data.size() == resultLines.size() - 1,
"Column size and CSV File row size are different!\nColumn: " + (data.size() + 1) + " CSV File: " + resultLines.size());
this.close();
Writer newwriter = new FileWriter(file.getPath());
for (String newline : resultLines) {
newwriter.write(newline + CSVWriter.DEFAULT_LINE_END);
}
newwriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public List<String[]> getData() throws IOException {
Reader reader = new FileReader(this.file);
CSVReader csvReader = new CSVReader(reader);
List<String[]> data = csvReader.readAll();
csvReader.close();
return data;
}
public void writeLines(List<String> header, List<List<String>> lines) throws IOException {
for (List<String> line : lines) {
writeLine(header, line);
}
}
public void writeLine(List<String> header, List<String> line) throws IOException {
if (this.writer == null) {
this.writer = new CSVWriteHandler(this.file.getPath(), header);
}
this.writer.writeLine(line);
}
public void close() throws IOException {
if (this.writer != null) {
writer.close();
this.writer = null;
}
}
// Takes mutiple CSV Files and glue them horizontically together.
public void combineCSVFiles(List<CSVHandler> csvFiles) throws IOException {
List<List<String[]>> fileList = new LinkedList<>();
for (CSVHandler csvHandler : csvFiles) {
fileList.add(csvHandler.getData());
}
boolean allEqualSize = fileList.stream()
.map(lineList -> lineList.size())
.distinct().count() == 1;
if (!allEqualSize) {
throw new IllegalArgumentException("[ERROR] Combining CSV Files failed due to files of different vertical length.");
}
// Map List<List<String[]>> to List<List<List<String>>>
Stream<List<List<String>>> listValues = fileList.stream()
.map(lineList -> lineList.stream()
.map(valueList -> Arrays.asList(valueList))
.collect(Collectors.toList()));
List<List<String>> resultLines = new LinkedList<>();
// Append each line to result lines
listValues.forEach(lineList -> {
for (int i = 0; i < lineList.size() ; i++) {
if (resultLines.size() <= i) {
resultLines.add(new LinkedList<String>());
}
resultLines.get(i).addAll(lineList.get(i));
}
});
List<String> header = resultLines.remove(0);
this.writeLines(header, resultLines);
}
public void stackCSVFiles(List<CSVHandler> csvFiles) throws IOException {
List<List<String[]>> fileList = new LinkedList<>();
List<String> header;
for (CSVHandler csvHandler : csvFiles) {
fileList.add(csvHandler.getData());
}
header = Arrays.asList(fileList.get(0).get(0));
List<List<String>> resultLines = fileList.stream()
.map(file -> {file.remove(0); return file;})
.flatMap(List::stream)
.map(lineArray -> Arrays.asList(lineArray))
.collect(Collectors.toList());
this.writeLines(header, resultLines);
}
}