-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSVUtil.java
174 lines (143 loc) · 4.06 KB
/
CSVUtil.java
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
package javaToolkit.lib.utils;
import java.io.FileWriter;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class CSVUtil {
public static void writeLine2Csv(List<String> strList, Path filePath) {
try {
FileWriter writer = new FileWriter(filePath.toString());
String collect = strList.stream().collect(Collectors.joining(","));
System.out.println(collect);
writer.write(collect);
writer.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void appendLine2Csv(List<String> strList, Path filePath) {
try {
String collect = strList.stream().collect(Collectors.joining(","));
// System.out.println(collect);
// pass true for appending
if (filePath.toFile().exists()) {
FileWriter writer = new FileWriter(filePath.toString(), true);
writer.write(collect);
writer.close();
} else {
FileWriter writer = new FileWriter(filePath.toString());
writer.write(collect);
writer.close();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* if " (double quotation mark) exists in a cell, then the cell needs to be
* replace("\"","\"\"") and add " before and after the cell
*
* the implementation is cell="\""+cellString.replace("\"","\"\"")+"\""
*
* @param strList
* @param filePath
*/
public static void write2DArray2Csv(List<List<String>> strList, Path filePath) {
try {
if (filePath.toFile().exists()) {
filePath.toFile().delete();
}
FileWriter writer = null;
for (List<String> line : strList) {
String collect = line.stream().collect(Collectors.joining(","));
// System.out.println(collect);
// pass true for appending
if (filePath.toFile().exists()) {
writer = new FileWriter(filePath.toString(), true);
writer.write(collect.trim() + "\n");
} else {
writer = new FileWriter(filePath.toString());
writer.write(collect.trim() + "\n");
}
writer.close();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* read csv line by line and parse single line to this function code example
* String csvFile = "/Users/mkyong/csv/country2.csv";
*
* Scanner scanner = new Scanner(new File(csvFile)); while
* (scanner.hasNext()) { List<String> line = parseLine(scanner.nextLine());
* System.out.println("Country [id= " + line.get(0) + ", code= " +
* line.get(1) + " , name=" + line.get(2) + "]"); } scanner.close();
*
* @param cvsLine
* @param separator
* @param quote
* @return
*/
public static List<String> parseLine(String cvsLine, char separator, char quote) {
List<String> result = new ArrayList<>();
// if empty, return!
if (cvsLine == null || cvsLine.isEmpty()) {
return result;
}
StringBuffer curVal = new StringBuffer();
boolean inQuotes = false;
boolean startCollectChar = false;
boolean doubleQuotesInColumn = false;
char[] chars = cvsLine.toCharArray();
for (char ch : chars) {
if (inQuotes) {
startCollectChar = true;
if (ch == quote) {
inQuotes = false;
doubleQuotesInColumn = false;
} else {
// Fixed : allow "" in custom quote enclosed
if (ch == '\"') {
if (!doubleQuotesInColumn) {
curVal.append(ch);
doubleQuotesInColumn = true;
}
} else {
curVal.append(ch);
}
}
} else {
if (ch == quote) {
inQuotes = true;
// Fixed : allow "" in empty quote enclosed
if (chars[0] != '"' && quote == '\"') {
curVal.append('"');
}
// double quotes in column will hit this!
if (startCollectChar) {
curVal.append('"');
}
} else if (ch == separator) {
result.add(curVal.toString());
curVal = new StringBuffer();
startCollectChar = false;
} else if (ch == '\r') {
// ignore LF characters
continue;
} else if (ch == '\n') {
// the end, break!
break;
} else {
curVal.append(ch);
}
}
}
result.add(curVal.toString());
return result;
}
}