Skip to content

Commit 92503c7

Browse files
authored
BAEL-9289 Update article "Reading a CSV File into an Array" (#18508)
* Create book-2.csv * Create ReadCSVInArrayUnitTest2.java * Update ReadCSVInArrayUnitTest2.java * Update book-2.csv * Update ReadCSVInArrayUnitTest2.java * Create book-3.csv * Update book-3.csv * Update ReadCSVInArrayUnitTest2.java * Delete core-java-modules/core-java-io-conversions/src/test/resources/book-3.csv * Update ReadCSVInArrayUnitTest.java * Update ReadCSVInArrayUnitTest.java * Update ReadCSVInArrayUnitTest2.java * Update ReadCSVInArrayUnitTest2.java * Update ReadCSVInArrayUnitTest.java * Update ReadCSVInArrayUnitTest.java * Update ReadCSVInArrayUnitTest.java * Update ReadCSVInArrayUnitTest.java * Delete core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/csv/ReadCSVInArrayUnitTest2.java * Update ReadCSVInArrayUnitTest.java * Rename book-2.csv to book2.csv * Update ReadCSVInArrayUnitTest.java * Update ReadCSVInArrayUnitTest.java * Update book2.csv * Update book2.csv * Update ReadCSVInArrayUnitTest.java * Update book2.csv * Update book2.csv * Update book2.csv * Update ReadCSVInArrayUnitTest.java * Update book2.csv * Update ReadCSVInArrayUnitTest.java * Update ReadCSVInArrayUnitTest.java * Update ReadCSVInArrayUnitTest.java * Create ReadCSVWithCommaInValuesIntoArrayUnitTest.java * Update ReadCSVInArrayUnitTest.java * Update ReadCSVWithCommaInValuesIntoArrayUnitTest.java * Update ReadCSVWithCommaInValuesIntoArrayUnitTest.java * Update ReadCSVWithCommaInValuesIntoArrayUnitTest.java * Update ReadCSVWithCommaInValuesIntoArrayUnitTest.java * Update ReadCSVWithCommaInValuesIntoArrayUnitTest.java * Update pom.xml * Update ReadCSVWithCommaInValuesIntoArrayUnitTest.java * Create book3.csv * Create ReadCSVWithCommaInValuesUsingOpenCSVIntoArrayUnitTest.java * Update ReadCSVWithCommaInValuesIntoArrayUnitTest.java * Delete core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/csv/ReadCSVWithCommaInValuesUsingOpenCSVIntoArrayUnitTest.java * Update ReadCSVWithCommaInValuesIntoArrayUnitTest.java * Create ReadCSVWithCommaInValuesUsingOpenCSVIntoArrayUnitTest.java * Update ReadCSVWithCommaInValuesIntoArrayUnitTest.java * Delete core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/csv/ReadCSVWithCommaInValuesUsingOpenCSVIntoArrayUnitTest.java * Update ReadCSVWithCommaInValuesIntoArrayUnitTest.java * Update book2.csv * Update ReadCSVWithCommaInValuesIntoArrayUnitTest.java * Update ReadCSVWithCommaInValuesIntoArrayUnitTest.java * Update ReadCSVWithCommaInValuesIntoArrayUnitTest.java * Create CustomCSVParserUnitTest.java * Update CustomCSVParserUnitTest.java * Update CustomCSVParserUnitTest.java
1 parent 6d4183d commit 92503c7

File tree

6 files changed

+262
-2
lines changed

6 files changed

+262
-2
lines changed

core-java-modules/core-java-io-conversions/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
</build>
3939

4040
<properties>
41-
<opencsv.version>5.8</opencsv.version>
41+
<opencsv.version>5.9</opencsv.version>
4242
</properties>
4343

44-
</project>
44+
</project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.baeldung.csv;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileNotFoundException;
6+
import java.io.FileReader;
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.Collections;
13+
import java.util.List;
14+
15+
import org.junit.Assert;
16+
import org.junit.Test;
17+
18+
19+
20+
public class CustomCSVParserUnitTest {
21+
public static final String COMMA_DELIMITER = ",";
22+
23+
public static final String CSV_FILE = "src/test/resources/book3.csv";
24+
25+
public static final List<List<String>> EXPECTED_ARRAY = Collections.unmodifiableList(new ArrayList<List<String>>() {
26+
{
27+
add(new ArrayList<String>() {
28+
{
29+
add("Kom, Mary");
30+
add("Unbreakable");
31+
}
32+
});
33+
add(new ArrayList<String>() {
34+
{
35+
add("Isapuari, Kapil");
36+
add("Farishta");
37+
}
38+
});
39+
}
40+
});
41+
42+
@Test
43+
public void givenCSVFileWithCommaInValues_whenCustomCSVParser_thenContentsAsExpected() throws IOException {
44+
List<List<String>> records = new ArrayList<List<String>>();
45+
try (BufferedReader br = new BufferedReader(new FileReader(CSV_FILE))) {
46+
String line = "";
47+
while ((line = br.readLine()) != null) {
48+
records.add(parseLine(line));
49+
}
50+
} catch (Exception e) {
51+
e.printStackTrace();
52+
}
53+
for (int i = 0; i < EXPECTED_ARRAY.size(); i++) {
54+
Assert.assertArrayEquals(EXPECTED_ARRAY.get(i)
55+
.toArray(),
56+
records.get(i)
57+
.toArray());
58+
}
59+
}
60+
61+
private static List<String> parseLine(String line) {
62+
List<String> values = new ArrayList<>();
63+
boolean inQuotes = false;
64+
StringBuilder currentValue = new StringBuilder();
65+
66+
for (char c : line.toCharArray()) {
67+
if (c == '"') {
68+
inQuotes = !inQuotes;
69+
} else if (c == ',' && !inQuotes) {
70+
values.add(currentValue.toString());
71+
currentValue = new StringBuilder();
72+
} else {
73+
currentValue.append(c);
74+
}
75+
}
76+
values.add(currentValue.toString());
77+
return values;
78+
}
79+
}

core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/csv/ReadCSVInArrayUnitTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class ReadCSVInArrayUnitTest {
4040
}
4141
});
4242

43+
4344
@Test
4445
public void givenCSVFile_whenBufferedReader_thenContentsAsExpected() throws IOException {
4546
List<List<String>> records = new ArrayList<List<String>>();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package com.baeldung.csv;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileNotFoundException;
6+
import java.io.FileReader;
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.Collections;
13+
import java.util.List;
14+
import java.util.Scanner;
15+
import java.util.stream.Collectors;
16+
import java.util.stream.Stream;
17+
18+
import org.junit.Assert;
19+
import org.junit.Test;
20+
21+
import com.opencsv.CSVReader;
22+
23+
public class ReadCSVWithCommaInValuesIntoArrayUnitTest {
24+
public static final String COMMA_DELIMITER = "\\|";
25+
public static final String CSV_FILE = "src/test/resources/book2.csv";
26+
public static final String CSV_FILE_OpenCSV = "src/test/resources/book3.csv";
27+
28+
public static final List<List<String>> EXPECTED_ARRAY = Collections.unmodifiableList(new ArrayList<List<String>>() {
29+
{
30+
add(new ArrayList<String>() {
31+
{
32+
add("\"Kom, Mary\"");
33+
add("Unbreakable");
34+
}
35+
});
36+
add(new ArrayList<String>() {
37+
{
38+
add("\"Isapuari, Kapil\"");
39+
add("Farishta");
40+
}
41+
});
42+
}
43+
});
44+
45+
public static final List<List<String>> EXPECTED_ARRAY_OpenCSV = Collections.unmodifiableList(new ArrayList<List<String>>() {
46+
{
47+
add(new ArrayList<String>() {
48+
{
49+
add("Kom, Mary");
50+
add("Unbreakable");
51+
}
52+
});
53+
add(new ArrayList<String>() {
54+
{
55+
add("Isapuari, Kapil");
56+
add("Farishta");
57+
}
58+
});
59+
}
60+
});
61+
62+
63+
@Test
64+
public void givenCSVFileWithCommaInValues_whenOpencsv_thenContentsAsExpected() throws IOException {
65+
List<List<String>> records = new ArrayList<List<String>>();
66+
try (CSVReader csvReader = new CSVReader(new FileReader(CSV_FILE_OpenCSV));) {
67+
String[] values = null;
68+
while ((values = csvReader.readNext()) != null) {
69+
records.add(Arrays.asList(values));
70+
}
71+
} catch (Exception e) {
72+
e.printStackTrace();
73+
}
74+
for (int i = 0; i < EXPECTED_ARRAY_OpenCSV.size(); i++) {
75+
Assert.assertArrayEquals(EXPECTED_ARRAY_OpenCSV.get(i)
76+
.toArray(),
77+
records.get(i)
78+
.toArray());
79+
}
80+
}
81+
82+
@Test
83+
public void givenCSVFileWithCommaInValues_whenBufferedReader_thenContentsAsExpected() throws IOException {
84+
List<List<String>> records = new ArrayList<List<String>>();
85+
try (BufferedReader br = new BufferedReader(new FileReader(CSV_FILE))) {
86+
String line = "";
87+
while ((line = br.readLine()) != null) {
88+
String[] values = line.split(COMMA_DELIMITER);
89+
records.add(Arrays.asList(values));
90+
}
91+
} catch (Exception e) {
92+
e.printStackTrace();
93+
}
94+
for (int i = 0; i < EXPECTED_ARRAY.size(); i++) {
95+
Assert.assertArrayEquals(EXPECTED_ARRAY.get(i)
96+
.toArray(),
97+
records.get(i)
98+
.toArray());
99+
}
100+
}
101+
102+
@Test
103+
public void givenCSVFileWithCommaInValues_whenScanner_thenContentsAsExpected() throws IOException {
104+
List<List<String>> records = new ArrayList<List<String>>();
105+
try (Scanner scanner = new Scanner(new File(CSV_FILE));) {
106+
while (scanner.hasNextLine()) {
107+
records.add(getRecordFromLine(scanner.nextLine()));
108+
}
109+
} catch (FileNotFoundException e) {
110+
e.printStackTrace();
111+
}
112+
for (int i = 0; i < EXPECTED_ARRAY.size(); i++) {
113+
Assert.assertArrayEquals(EXPECTED_ARRAY.get(i)
114+
.toArray(),
115+
records.get(i)
116+
.toArray());
117+
}
118+
}
119+
120+
private List<String> getRecordFromLine(String line) {
121+
List<String> values = new ArrayList<String>();
122+
try (Scanner rowScanner = new Scanner(line)) {
123+
rowScanner.useDelimiter(COMMA_DELIMITER);
124+
while (rowScanner.hasNext()) {
125+
values.add(rowScanner.next());
126+
}
127+
}
128+
return values;
129+
}
130+
131+
@Test
132+
public void givenCSVFileWithCommaInValues_whenUsingFilesReadAllLinesMethod_thenContentsAsExpected() throws IOException {
133+
List<List<String>> records = Files.readAllLines(Paths.get(CSV_FILE))
134+
.stream()
135+
.map(line -> Arrays.asList(line.split(COMMA_DELIMITER)))
136+
.collect(Collectors.toList());
137+
138+
for (int i = 0; i < EXPECTED_ARRAY.size(); i++) {
139+
Assert.assertArrayEquals(EXPECTED_ARRAY.get(i)
140+
.toArray(),
141+
records.get(i)
142+
.toArray());
143+
}
144+
}
145+
146+
@Test
147+
public void givenCSVFileWithCommaInValues_whenUsingFilesNewBufferedReaderMethod_thenContentsAsExpected() throws IOException {
148+
try (BufferedReader reader = Files.newBufferedReader(Paths.get(CSV_FILE))) {
149+
List<List<String>> records = reader.lines()
150+
.map(line -> Arrays.asList(line.split(COMMA_DELIMITER)))
151+
.collect(Collectors.toList());
152+
153+
for (int i = 0; i < EXPECTED_ARRAY.size(); i++) {
154+
Assert.assertArrayEquals(EXPECTED_ARRAY.get(i)
155+
.toArray(),
156+
records.get(i)
157+
.toArray());
158+
}
159+
}
160+
}
161+
162+
@Test
163+
public void givenCSVFileWithCommaInValues_whenUsingFilesLinesMethod_thenContentsAsExpected() throws IOException {
164+
try (Stream<String> lines = Files.lines(Paths.get(CSV_FILE))) {
165+
List<List<String>> records = lines.map(line -> Arrays.asList(line.split(COMMA_DELIMITER)))
166+
.collect(Collectors.toList());
167+
168+
for (int i = 0; i < EXPECTED_ARRAY.size(); i++) {
169+
Assert.assertArrayEquals(EXPECTED_ARRAY.get(i)
170+
.toArray(),
171+
records.get(i)
172+
.toArray());
173+
}
174+
}
175+
}
176+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"Kom, Mary"|Unbreakable
2+
"Isapuari, Kapil"|Farishta
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"Kom, Mary",Unbreakable
2+
"Isapuari, Kapil",Farishta

0 commit comments

Comments
 (0)