Skip to content

Commit 923c5a3

Browse files
committed
read csv
1 parent 76ca8a2 commit 923c5a3

15 files changed

Lines changed: 243 additions & 462 deletions

File tree

java-io/src/main/java/com/mkyong/io/csv/CsvFileReader.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

java-io/src/main/java/com/mkyong/io/csv/CsvFileReaderOpenCsv.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

java-io/src/main/java/com/mkyong/io/csv/CsvParserSimple.java

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,55 +31,71 @@ public class CsvParserSimple {
3131
private static final char DEFAULT_SEPARATOR = ',';
3232
private static final char DOUBLE_QUOTES = '"';
3333
private static final char DEFAULT_QUOTE_CHAR = DOUBLE_QUOTES;
34+
private static final String NEW_LINE = "\n";
3435

3536
private boolean isMultiLine = false;
3637
private String pendingField = "";
3738
private String[] pendingFieldLine = new String[]{};
3839

39-
private boolean isFieldStarted = false;
40-
private boolean isFieldClosed = false;
41-
4240
public static void main(String[] args) throws Exception {
4341

44-
URL resource = CsvParserSimple.class.getClassLoader().getResource("csv/wikipedia.csv");
42+
//URL resource = CsvParserSimple.class.getClassLoader().getResource("csv/wikipedia.csv");
43+
44+
// Loads file from resources folder
45+
URL resource = CsvParserSimple.class.getClassLoader().getResource("csv/monitor.csv");
4546
File file = Paths.get(resource.toURI()).toFile();
4647

4748
CsvParserSimple obj = new CsvParserSimple();
48-
List<String[]> result = obj.readFile(file);
49-
result.forEach(x -> System.out.println(Arrays.toString(x)));
49+
List<String[]> result = obj.readFile(file, 1);
50+
51+
int listIndex = 0;
52+
for (String[] arrays : result) {
53+
System.out.println("\nString[" + listIndex++ + "] : " + Arrays.toString(arrays));
54+
55+
int index = 0;
56+
for (String array : arrays) {
57+
System.out.println(index++ + " : " + array);
58+
}
59+
60+
}
61+
62+
}
5063

64+
public List<String[]> readFile(File csvFile) throws Exception {
65+
return readFile(csvFile, 0);
5166
}
5267

53-
public List<String[]> readFile(File csvfile) throws Exception {
68+
public List<String[]> readFile(File csvFile, int skipLine) throws Exception {
5469

5570
List<String[]> result = new ArrayList<>();
71+
int indexLine = 1;
5672

57-
try (BufferedReader br = new BufferedReader(new FileReader(csvfile))) {
73+
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
5874

5975
String line;
6076
while ((line = br.readLine()) != null) {
6177

78+
if (indexLine++ <= skipLine) {
79+
continue;
80+
}
81+
6282
String[] csvLineInArray = parseLine(line);
6383

64-
// multiline
65-
if (pendingField.length() > 0) {
66-
pendingFieldLine = Arrays.copyOf(csvLineInArray, csvLineInArray.length);
84+
if (isMultiLine) {
85+
pendingFieldLine = joinArrays(pendingFieldLine, csvLineInArray);
6786
} else {
6887

69-
// join two arrays
7088
if (pendingFieldLine != null && pendingFieldLine.length > 0) {
71-
String[] r = Stream.concat(Arrays.stream(pendingFieldLine), Arrays.stream(csvLineInArray))
72-
.toArray(String[]::new);
73-
74-
result.add(r);
75-
pendingFieldLine = null;
89+
// joins all fields and add to list
90+
result.add(joinArrays(pendingFieldLine, csvLineInArray));
91+
pendingFieldLine = new String[]{};
7692
} else {
93+
// if dun want to support multiline, only this line is required.
7794
result.add(csvLineInArray);
7895
}
7996

8097
}
8198

82-
8399
}
84100
}
85101

@@ -103,58 +119,57 @@ private List<String> parse(String line, char separator, char quoteChar) throws E
103119

104120
StringBuilder field = new StringBuilder();
105121

106-
// convert line to char[] and loop one by one
107122
for (char c : line.toCharArray()) {
108123

109-
// handle embedded double quotes ""
110-
if (c == DOUBLE_QUOTES) {
124+
if (c == DOUBLE_QUOTES) { // handle embedded double quotes ""
111125
if (isFieldWithEmbeddedDoubleQuotes) {
112-
field.append(DOUBLE_QUOTES);
113-
isFieldWithEmbeddedDoubleQuotes = false;
126+
127+
if (field.length() > 0) { // handle for empty field like "",""
128+
field.append(DOUBLE_QUOTES);
129+
isFieldWithEmbeddedDoubleQuotes = false;
130+
}
131+
114132
} else {
115133
isFieldWithEmbeddedDoubleQuotes = true;
116134
}
117135
} else {
118136
isFieldWithEmbeddedDoubleQuotes = false;
119137
}
120138

121-
// for multiline
122-
// any pending from the previous field?
123-
if (pendingField.length() > 0) {
124-
field.append(pendingField);
139+
if (isMultiLine) { // multiline, add pending from the previous field
140+
field.append(pendingField).append(NEW_LINE);
125141
pendingField = "";
126142
inQuotes = true;
143+
isMultiLine = false;
127144
}
128145

129-
// General parsing
130146
if (c == quoteChar) {
131147
inQuotes = !inQuotes;
132148
} else {
133-
// if find separator and not in quotes, add field to a list's entry
134-
if (c == separator && !inQuotes) {
149+
if (c == separator && !inQuotes) { // if find separator and not in quotes, add field to the list
135150
result.add(field.toString());
136-
field.setLength(0);
151+
field.setLength(0); // empty the field and ready for the next
137152
} else {
138-
// else append the char into a field
139-
field.append(c);
153+
field.append(c); // else append the char into a field
140154
}
141155
}
142156

143157
}
144158

145-
//line done, what to do?
159+
//line done, what to do next?
146160
if (inQuotes) {
147-
// multiline
148-
pendingField = field.toString();
161+
pendingField = field.toString(); // multiline
162+
isMultiLine = true;
149163
} else {
150-
//if remaining, add to result
151-
if (field.length() > 0) {
152-
result.add(field.toString());
153-
}
164+
result.add(field.toString()); // this is the last field
154165
}
155166

156167
return result;
157168

158169
}
159170

171+
private String[] joinArrays(String[] array1, String[] array2) {
172+
return Stream.concat(Arrays.stream(array1), Arrays.stream(array2))
173+
.toArray(String[]::new);
174+
}
160175
}

java-io/src/main/java/com/mkyong/io/csv/SingleClassCsvReader.java

Lines changed: 0 additions & 132 deletions
This file was deleted.

0 commit comments

Comments
 (0)