-
Notifications
You must be signed in to change notification settings - Fork 67
/
CSVWriter.java
executable file
·162 lines (145 loc) · 5.25 KB
/
CSVWriter.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
/*
* Copyright 2012-2014 Dristhi software
* Copyright 2015 Arkni Brahim
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.jsontocsv.writer;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
public class CSVWriter {
/**
* The class logger
*/
private static final Logger LOGGER = Logger.getLogger(CSVWriter.class);
/**
* Convert the given List of String keys-values as a CSV String.
*
* @param flatJson The List of key-value pairs generated from the JSON String
*
* @return The generated CSV string
*/
public static String getCSV(List<Map<String, String>> flatJson) {
// Use the default separator
return getCSV(flatJson, ",");
}
/**
* Convert the given List of String keys-values as a CSV String.
*
* @param flatJson The List of key-value pairs generated from the JSON String
* @param separator The separator can be: ',', ';' or '\t'
*
* @return The generated CSV string
*/
public static String getCSV(List<Map<String, String>> flatJson, String separator) {
Set<String> headers = collectHeaders(flatJson);
String csvString = StringUtils.join(headers.toArray(), separator) + "\n";
for (Map<String, String> map : flatJson) {
csvString = csvString + getSeperatedColumns(headers, map, separator) + "\n";
}
return csvString;
}
/**
* Write the given CSV string to the given file.
*
* @param csvString The csv string to write into the file
* @param fileName The file to write (included the path)
*/
public static void writeToFile(String csvString, String fileName) {
try {
FileUtils.write(new File(fileName), csvString);
} catch (IOException e) {
LOGGER.error("CSVWriter#writeToFile(csvString, fileName) IOException: ", e);
}
}
/**
* Write the given CSV from a flat json to the given file.
*
* @param flatJson
* @param separator
* @param fileName
* @param headers
*/
public static void writeLargeFile(List<Map<String, String>> flatJson, String separator, String fileName, Set<String> headers){
String csvString;
csvString = StringUtils.join(headers.toArray(), separator) + "\n";
File file = new File(fileName);
try {
// ISO8859_1 char code to Latin alphabet
FileUtils.write(file, csvString, "ISO8859_1");
for (Map<String, String> map : flatJson) {
csvString = "";
csvString = getSeperatedColumns(headers, map, separator) + "\n";
Files.write(Paths.get(fileName), csvString.getBytes("ISO8859_1"), StandardOpenOption.APPEND);
}
} catch (IOException e) {
LOGGER.error("CSVWriter#writeLargeFile(flatJson, separator, fileName, headers) IOException: ", e);
}
}
/**
* Get separated comlumns used a separator (comma, semi column, tab).
*
* @param headers The CSV headers
* @param map Map of key-value pairs contains the header and the value
*
* @return a string composed of columns separated by a specific separator.
*/
private static String getSeperatedColumns(Set<String> headers, Map<String, String> map, String separator) {
List<String> items = new ArrayList<String>();
for (String header : headers) {
String value = map.get(header) == null ? "" : map.get(header).replaceAll("[\\,\\;\\r\\n\\t\\s]+", " ");
items.add(value);
}
return StringUtils.join(items.toArray(), separator);
}
/**
* Get the CSV header.
*
* @param flatJson
*
* @return a Set of headers
*/
private static Set<String> collectHeaders(List<Map<String, String>> flatJson) {
Set<String> headers = new LinkedHashSet<String>();
for (Map<String, String> map : flatJson) {
headers.addAll(map.keySet());
}
return headers;
}
/**
* Get the CSV ordered header
*
* @param flatJson
*
* @return a Set of ordered headers
*/
public static Set<String> collectOrderedHeaders(List<Map<String, String>> flatJson) {
Set<String> headers = new TreeSet<String>();
for (Map<String, String> map : flatJson) {
headers.addAll(map.keySet());
}
return headers;
}
}