-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathCSVFile.java
More file actions
38 lines (30 loc) · 925 Bytes
/
CSVFile.java
File metadata and controls
38 lines (30 loc) · 925 Bytes
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
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.Reader;
import java.util.stream.*;
import model.Person;
public class CSVFile {
public static void main(String[] args) {
Path path = Paths.get("Files/data.csv");
try (Stream<String> lines = Files.lines(path)) {
lines.filter(line -> !line.startsWith("#")).flatMap(CSVFile::lineToPerson).forEach(System.out::println);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private static Stream<Person> lineToPerson(String line) {
try {
String[] elements = line.split(";");
String name = elements[0];
int age = Integer.parseInt(elements[1]);
String city = elements[2];
Person p = new Person(name, age, city);
return Stream.of(p);
} catch (Exception e) {
}
return Stream.empty();
}
}