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 ("\n Length : " + 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 ("\n Length : " + 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 ("\n Length : " + 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