6
6
import java .io .Reader ;
7
7
import java .nio .file .Files ;
8
8
import java .nio .file .Paths ;
9
+ import java .util .Iterator ;
9
10
import java .util .List ;
10
11
11
12
/**
@@ -24,15 +25,16 @@ public static void main(String[] args) throws IOException {
24
25
String [] memberFieldsToBindTo = {"name" , "email" , "phoneNo" , "country" };
25
26
strategy .setColumnMapping (memberFieldsToBindTo );
26
27
27
- CsvToBean csvToBean = new CsvToBeanBuilder (reader )
28
+ CsvToBean < MyUser > csvToBean = new CsvToBeanBuilder (reader )
28
29
.withMappingStrategy (strategy )
29
30
.withSkipLines (1 )
30
31
.withIgnoreLeadingWhiteSpace (true )
31
32
.build ();
32
33
33
- List <MyUser > myUsers = csvToBean .parse ();
34
+ Iterator <MyUser > myUserIterator = csvToBean .iterator ();
34
35
35
- for (MyUser myUser : myUsers ) {
36
+ while (myUserIterator .hasNext ()) {
37
+ MyUser myUser = myUserIterator .next ();
36
38
System .out .println ("Name : " + myUser .getName ());
37
39
System .out .println ("Email : " + myUser .getEmail ());
38
40
System .out .println ("PhoneNo : " + myUser .getPhoneNo ());
@@ -41,6 +43,19 @@ public static void main(String[] args) throws IOException {
41
43
}
42
44
}
43
45
}
46
+
47
+ // Reads all CSV contents into memory (Not suitable for large CSV files)
48
+ private static void readAllBeansAtOnce (CsvToBean csvToBean ) {
49
+ List <MyUser > myUsers = csvToBean .parse ();
50
+
51
+ for (MyUser myUser : myUsers ) {
52
+ System .out .println ("Name : " + myUser .getName ());
53
+ System .out .println ("Email : " + myUser .getEmail ());
54
+ System .out .println ("PhoneNo : " + myUser .getPhoneNo ());
55
+ System .out .println ("Country : " + myUser .getCountry ());
56
+ System .out .println ("---------------------------" );
57
+ }
58
+ }
44
59
}
45
60
46
61
0 commit comments