Skip to content

Commit 49c3052

Browse files
committed
csv
1 parent f851ad0 commit 49c3052

14 files changed

Lines changed: 598 additions & 0 deletions

File tree

java-io/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@
6868
<version>1.20</version>
6969
</dependency>
7070

71+
<!-- csv file -->
72+
<dependency>
73+
<groupId>com.opencsv</groupId>
74+
<artifactId>opencsv</artifactId>
75+
<version>5.3</version>
76+
</dependency>
77+
7178
</dependencies>
7279

7380
<build>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.mkyong.io.csv;
2+
3+
import java.io.File;
4+
import java.util.List;
5+
6+
public interface CsvFileReader {
7+
List<String[]> read(File file) throws Exception;
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.mkyong.io.csv;
2+
3+
import com.opencsv.CSVReader;
4+
5+
import java.io.File;
6+
import java.io.FileReader;
7+
import java.util.List;
8+
9+
public class CsvFileReaderOpenCsv implements CsvFileReader {
10+
11+
@Override
12+
public List<String[]> read(File file) throws Exception {
13+
List<String[]> result;
14+
try (CSVReader reader = new CSVReader(new FileReader(file))) {
15+
result = reader.readAll();
16+
}
17+
return result;
18+
}
19+
20+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.mkyong.io.csv;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* https://tools.ietf.org/html/rfc4180
9+
* <p>
10+
* Fields containing line breaks (CRLF), double quotes, and commas
11+
* should be enclosed in double-quotes. For example:
12+
* <p>
13+
* "aaa","b CRLF
14+
* bb","ccc" CRLF
15+
* zzz,yyy,xxx
16+
* <p>
17+
* If double-quotes are used to enclose fields, then a double-quote
18+
* appearing inside a field must be escaped by preceding it with
19+
* another double quote. For example:
20+
* <p>
21+
* "aaa","b""bb","ccc"
22+
*/
23+
public class CsvParserSimple {
24+
25+
private static final char DEFAULT_SEPARATOR = ',';
26+
private static final char DEFAULT_QUOTE_CHARACTER = '"';
27+
28+
public String[] parseLine(String line) throws IOException {
29+
return parseLine(line, DEFAULT_SEPARATOR);
30+
}
31+
32+
public String[] parseLine(String line, char separator) throws IOException {
33+
return parseLine(line, separator, DEFAULT_QUOTE_CHARACTER);
34+
}
35+
36+
public String[] parseLine(String line, char separator, char quoteChar) throws IOException {
37+
return parse(line, separator, quoteChar).toArray(String[]::new);
38+
}
39+
40+
private List<String> parse(String line, char separator, char quoteChar) {
41+
42+
List<String> result = new ArrayList<>();
43+
44+
boolean inQuotes = false;
45+
boolean startCollectChar = false;
46+
boolean doubleQuotesInColumn = false;
47+
48+
StringBuilder field = new StringBuilder();
49+
50+
// convert line to char[] and loop one by one
51+
for (char c : line.toCharArray()) {
52+
53+
if (c == quoteChar) {
54+
inQuotes = !inQuotes;
55+
} else {
56+
// if find separator and not in quotes, add chars to a field.
57+
if (c == separator && !inQuotes) {
58+
result.add(field.toString());
59+
field.setLength(0);
60+
} else {
61+
// else add the char into a field
62+
field.append(c);
63+
}
64+
}
65+
66+
}
67+
68+
//line done, what to do? if remaining, add to result
69+
if (field.length() > 0) {
70+
result.add(field.toString());
71+
}
72+
73+
return result;
74+
75+
}
76+
77+
78+
/*public static List<List<String>> read(File file) throws Exception {
79+
80+
List<List<String>> result = new ArrayList<>();
81+
82+
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
83+
84+
String line;
85+
while ((line = br.readLine()) != null) {
86+
List<String> strings = parseLine(line, ',', '"');
87+
result.add(strings);
88+
}
89+
}
90+
91+
return result;
92+
}*/
93+
94+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.mkyong.io.csv;
2+
3+
import java.io.BufferedReader;
4+
import java.io.Reader;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
public class SingleClassCsvReader {
9+
10+
public static final char LF = '\n';
11+
public static final char CR = '\r';
12+
public static final char COMMA = ',';
13+
public static final char DOUBLE_QUOTE = '"';
14+
15+
/**
16+
* Core implementation for reading csv-values from a {@link java.io.Reader}
17+
* @param reader Source for reading.
18+
* @param delimiter Delimiter character. Default is a comma(,).
19+
* @param quote Quote character. Default is a double quote char(").
20+
* @return List of read Csv lines.
21+
* @throws Exception If an error happens whilst reading.
22+
*/
23+
public static List<List<String>> read(Reader reader, char delimiter, char quote) throws Exception{
24+
if(reader == null){
25+
throw new Exception("missing reader",
26+
new IllegalArgumentException(new NullPointerException("reader")));
27+
}
28+
if(delimiter == ' '){
29+
delimiter = COMMA;
30+
}
31+
if(quote == ' '){
32+
quote = DOUBLE_QUOTE;
33+
}
34+
BufferedReader buff = null;
35+
try {
36+
//Create an underlying reader to keep original reader's reference
37+
buff = new BufferedReader(reader);
38+
39+
List<List<String>> csv = new LinkedList<List<String>>();
40+
String line = null;
41+
while((line = buff.readLine()) != null){
42+
csv.add(readLine(line, delimiter, quote));
43+
}
44+
return csv;
45+
}catch(Exception e){
46+
throw e;
47+
}finally{
48+
if(buff != null){
49+
try{
50+
buff.close();
51+
buff = null;
52+
}catch(Exception e){
53+
throw e;
54+
}
55+
}
56+
}
57+
}
58+
59+
/**
60+
* Core implementation for reading csv-values within text line.
61+
* @param line Csv text line
62+
* @param delimiter Delimiter character. Default is a comma(,).
63+
* @param quote Quote character. Default is a double quote char(").
64+
* @return List of read Csv values.
65+
*/
66+
public static List<String> readLine(String line, char delimiter, char quote) {
67+
if(line == null)
68+
line = "";
69+
if(delimiter == ' ')
70+
delimiter = COMMA;
71+
if(quote == ' ')
72+
quote = DOUBLE_QUOTE;
73+
74+
List<String> values = new LinkedList<String>();
75+
char[] chars = line.toCharArray();
76+
77+
StringBuffer value = new StringBuffer("");
78+
boolean quoted = false;
79+
80+
for (int i = 0; i < chars.length; i++) {
81+
char c = chars[i];
82+
if(CR == c)
83+
continue;
84+
85+
if(LF == c) {
86+
values.add(value.toString());
87+
value = new StringBuffer();
88+
break;
89+
}
90+
91+
if(quoted) {
92+
if(quote == c) {
93+
if((i+1) < chars.length && chars[i+1] == quote) {
94+
value.append(quote);
95+
i++;
96+
}else {
97+
quoted = false;
98+
}
99+
}else if(delimiter == c) {
100+
value.append(c);
101+
}else {
102+
value.append(c);
103+
}
104+
}else {
105+
if(quote == c) {
106+
quoted = true;
107+
if(
108+
((i+1) < chars.length && chars[i+1] == delimiter) &&
109+
((i-1) >= 0 && chars[i-1] != delimiter)
110+
) {
111+
quoted = false;
112+
value.append(c);
113+
}
114+
115+
} else if(delimiter == c) {
116+
values.add(value.toString());
117+
value = new StringBuffer();
118+
} else{
119+
value.append(c);
120+
}
121+
}
122+
123+
}
124+
125+
if(value.length()>0) {
126+
values.add(value.toString());
127+
}
128+
129+
return values;
130+
}
131+
132+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.mkyong.io.csv.test;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.util.List;
8+
import java.util.stream.Collectors;
9+
import java.util.stream.Stream;
10+
11+
public class CSVReaderSimple {
12+
13+
private static final String CSV_SEPARATOR = ",";
14+
15+
public static void main(String[] args) {
16+
17+
//readCSVFile("csv/country.csv", CSV_SEPARATOR);
18+
readCSVFileJava8("csv/address.csv", CSV_SEPARATOR);
19+
}
20+
21+
private static void readCSVFileJava8(String fileName, String csvSeparator) {
22+
23+
String line;
24+
25+
try (BufferedReader br = new BufferedReader(
26+
new InputStreamReader(
27+
CSVReaderSimple.class.getClassLoader().getResourceAsStream(fileName)))) {
28+
29+
while ((line = br.readLine()) != null) {
30+
31+
// split by a comma separator
32+
List<String> split = Stream.of(line.split(csvSeparator)).collect(Collectors.toList());
33+
34+
System.out.println("\nLength : " + split.size());
35+
split.forEach(System.out::println);
36+
37+
}
38+
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
}
42+
43+
}
44+
45+
private static void readCSVFile(String fileName, String csvSeparator) {
46+
47+
String line;
48+
try (BufferedReader br = new BufferedReader(
49+
new InputStreamReader(
50+
CSVReaderSimple.class.getClassLoader().getResourceAsStream(fileName)))) {
51+
52+
while ((line = br.readLine()) != null) {
53+
54+
// split by a comma separator
55+
String[] split = line.split(csvSeparator);
56+
System.out.println("\nLength : " + split.length);
57+
System.out.println("split[0] : " + split[0]);
58+
System.out.println("split[1] : " + split[1]);
59+
System.out.println("split[2] : " + split[2]);
60+
System.out.println("split[3] : " + split[3]);
61+
System.out.println("split[4] : " + split[4]);
62+
System.out.println("split[5] : " + split[5]);
63+
64+
}
65+
66+
} catch (IOException e) {
67+
e.printStackTrace();
68+
}
69+
70+
}
71+
72+
private static void readCSVFileReader() {
73+
74+
String csvFile = "/Users/mkyong/csv/country.csv";
75+
String line;
76+
String csvSeparator = ",";
77+
78+
// auto close file
79+
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
80+
81+
while ((line = br.readLine()) != null) {
82+
83+
// split by a comma separator
84+
String[] split = line.split(csvSeparator);
85+
System.out.println("\nLength : " + split.length);
86+
System.out.println("split[0] : " + split[0]);
87+
System.out.println("split[1] : " + split[1]);
88+
System.out.println("split[2] : " + split[2]);
89+
System.out.println("split[3] : " + split[3]);
90+
System.out.println("split[4] : " + split[4]);
91+
System.out.println("split[5] : " + split[5]);
92+
93+
}
94+
95+
} catch (IOException e) {
96+
e.printStackTrace();
97+
}
98+
99+
}
100+
101+
}

0 commit comments

Comments
 (0)