How to Use Java to Read All Csv Files in a Folder
In an earlier article, I wrote near how to read and write CSV files in Coffee using Apache Eatables CSV.
In this article, I'll have you through another open up source library called OpenCSV for reading and writing CSV files in Java.
Adding OpenCSV dependency
First of all, you lot need to add the OpenCSV dependency in your projection. If you're a Maven user, add together the following dependency to your pom.xml
file.
<dependency > <groupId > com.opencsv </groupId > <artifactId > opencsv </artifactId > <version > 4.0 </version > </dependency >
And here is the dependency for Gradle users -
compile "com.opencsv:opencsv:four.0"
Sample CSV file
Following are two sample CSV files that nosotros'll read and parse in the examples presented in this article.
CSV file without a header - users.csv
Rajeev Kumar Singh ♥,rajeevs@case.com,+91-9999999999,India Sachin Tendulkar,sachin@example.com,+91-9999999998,India Barak Obama,barak.obama@instance.com,+one-1111111111,United States Donald Trump,donald.trump@example.com,+ane-2222222222,The states
CSV file with a header - users-with-header.csv
proper name,e-mail,telephone,country Rajeev Kumar Singh ♥,rajeevs@example.com,+91-9999999999,India Sachin Tendulkar,sachin@case.com,+91-9999999998,India Barak Obama,barak.obama@case.com,+1-1111111111,U.s. Donald Trump,donald.trump@case.com,+ane-2222222222,Us
Read a CSV file (Think each tape every bit a String array)
The example below shows how to read and parse a CSV file using OpenCSV library. It reads the CSV records one by one into a String array -
import com.opencsv. CSVReader ; import java.io. IOException ; import java.io. Reader ; import java.nio.file. Files ; import java.nio.file. Paths ; public class OpenCSVReader { private static terminal String SAMPLE_CSV_FILE_PATH = "./users.csv" ; public static void main ( String [ ] args) throws IOException { attempt ( Reader reader = Files . newBufferedReader ( Paths . get (SAMPLE_CSV_FILE_PATH) ) ; CSVReader csvReader = new CSVReader (reader) ; ) { // Reading Records 1 by One in a String array String [ ] nextRecord; while ( (nextRecord = csvReader. readNext ( ) ) != null ) { System .out. println ( "Name : " + nextRecord[ 0 ] ) ; Organization .out. println ( "Email : " + nextRecord[ 1 ] ) ; System .out. println ( "Phone : " + nextRecord[ 2 ] ) ; System .out. println ( "Country : " + nextRecord[ three ] ) ; System .out. println ( "==========================" ) ; } } } }
Reading all records at one time
In the higher up case, We read the CSV records one by i using the readNext()
method. CSVReader
also provides a method chosen readAll()
to read all the records at one time into a List<String[]>
.
// Reading All Records at in one case into a Listing<String[]> List < Cord [ ] > records = csvReader. readAll ( ) ; for ( String [ ] tape : records) { Organization .out. println ( "Name : " + record [ 0 ] ) ; System .out. println ( "E-mail : " + record [ 1 ] ) ; System .out. println ( "Phone : " + record [ ii ] ) ; Organization .out. println ( "Country : " + record [ 3 ] ) ; System .out. println ( "---------------------------" ) ; }
Notation that, the above method loads the entire CSV contents into retention, and therefore is not suitable for big CSV files.
If you endeavour to read the Sample CSV file that contains a header, so the header record will also be printed in the output. If you want to skip the header row, so you can apply a CSVReaderBuilder
class to construct a CSVReader
with the specified number of lines skipped.
import com.opencsv. CSVReaderBuilder ; CSVReader csvReader = new CSVReaderBuilder (reader) . withSkipLines ( 1 ) . build ( ) ;
Read a CSV file and parse the records into a Java Object
The real strength of OpenCSV library is that you tin directly parse CSV records into Coffee objects. In that location are two ways of doing it - The commencement method makes use of annotations and the second method uses Mapping strategies.
At that place are two types of annotations in OpenCSV - @CsvBindByName
and @CsvBindByPosition
. You lot can use these annotations to specify which CSV column should be spring to which member field of the Java object.
If the CSV file contains a header, then you can utilise @CsvBindByName
annotation to specify the mapping between the CSV columns and the member fields.
The @CsvBindByName
note accepts iii parameters - column, required and locale. The required
and locale
parameters are optional, and you can omit the column
parameter as well if the header proper name in the CSV file is same as the member field proper noun.
Here is an example of a POJO class that makes use of @CsvBindByName
annotations -
import com.opencsv.edible bean. CsvBindByName ; public class CSVUser { @CsvBindByName individual String proper name; @CsvBindByName (column = "email" , required = true ) private String email; @CsvBindByName (column = "telephone" ) private String phoneNo; @CsvBindByName private String land; // Getters and Setters (Omitted for brevity) }
The case below shows how to read and parse the CSV records directly into your Java objects -
import com.opencsv.edible bean. CsvToBean ; import com.opencsv.bean. CsvToBeanBuilder ; import java.io. IOException ; import java.io. Reader ; import java.nio.file. Files ; import java.nio.file. Paths ; import java.util. Iterator ; import java.util. List ; public class OpenCSVReadAndParseToBean { private static final String SAMPLE_CSV_FILE_PATH = "./users-with-header.csv" ; public static void master ( String [ ] args) throws IOException { endeavor ( Reader reader = Files . newBufferedReader ( Paths . become (SAMPLE_CSV_FILE_PATH) ) ; ) { CsvToBean < CSVUser > csvToBean = new CsvToBeanBuilder (reader) . withType ( CSVUser . class ) . withIgnoreLeadingWhiteSpace ( truthful ) . build ( ) ; Iterator < CSVUser > csvUserIterator = csvToBean. iterator ( ) ; while (csvUserIterator. hasNext ( ) ) { CSVUser csvUser = csvUserIterator. next ( ) ; Organization .out. println ( "Name : " + csvUser. getName ( ) ) ; System .out. println ( "E-mail : " + csvUser. getEmail ( ) ) ; System .out. println ( "PhoneNo : " + csvUser. getPhoneNo ( ) ) ; System .out. println ( "State : " + csvUser. getCountry ( ) ) ; Arrangement .out. println ( "==========================" ) ; } } } }
In the above instance, we obtained an Iterator
from csvToBean
object, and and so looped through this iterator to recall every object ane by 1.
The CsvToBean
class also provides a parse()
method which parses the unabridged CSV file and loads all the objects at once into retention. You tin can apply it like so -
// Reads all CSV contents into memory (Not suitable for large CSV files) List < CSVUser > csvUsers = csvToBean. parse ( ) ; for ( CSVUser csvUser: csvUsers) { Arrangement .out. println ( "Name : " + csvUser. getName ( ) ) ; System .out. println ( "Email : " + csvUser. getEmail ( ) ) ; System .out. println ( "PhoneNo : " + csvUser. getPhoneNo ( ) ) ; Arrangement .out. println ( "Country : " + csvUser. getCountry ( ) ) ; System .out. println ( "==========================" ) ; }
Manifestly, the above method is non suitable for significantly large CSV files because it loads the unabridged CSV file contents into memory.
Using @CsvBindByPosition annotation
If your CSV file doesn't comprise a header, then yous can employ @CsvBindByPosition
annotation to specify the mappings like this -
import com.opencsv.bean. CsvBindByPosition ; public class CSVUser { @CsvBindByPosition (position = 0 ) private String name; @CsvBindByPosition (position = 1 ) individual Cord email; @CsvBindByPosition (position = ii ) individual Cord phoneNo; @CsvBindByPosition (position = 3 ) private String land; // Getters and Setters (Omitted for brevity) }
Read a CSV file and parse the records into a Java object without using annotations
If you lot don't desire to clutter your POJO class with OpenCSV annotations, then you lot tin can utilise Mapping strategies to specify the mapping between CSV columns and object member fields.
Consider the following MyUser
class.
public class MyUser { private String proper noun; private Cord email; private String phoneNo; individual String country; public MyUser ( ) { } public MyUser ( String name, String e-mail, String phoneNo, String country) { this .name = name; this .electronic mail = email; this .phoneNo = phoneNo; this .country = country; } // Getters and Setters (Omitted for brevity) }
Here is how yous can use a ColumnPositionMappingStrategy
to specify the mapping between CSV columns and Java object's fellow member fields, and parse the CSV records into Coffee objects.
import com.opencsv.bean. ColumnPositionMappingStrategy ; import com.opencsv.edible bean. CsvToBean ; import com.opencsv.bean. CsvToBeanBuilder ; import java.io. IOException ; import java.io. Reader ; import java.nio.file. Files ; import java.nio.file. Paths ; import java.util. Iterator ; import java.util. Listing ; public course OpenCSVParseToBeanWithoutAnnotation { individual static concluding Cord SAMPLE_CSV_FILE_PATH = "./users-with-header.csv" ; public static void main ( String [ ] args) throws IOException { try ( Reader reader = Files . newBufferedReader ( Paths . become (SAMPLE_CSV_FILE_PATH) ) ; ) { ColumnPositionMappingStrategy strategy = new ColumnPositionMappingStrategy ( ) ; strategy. setType ( MyUser . course ) ; String [ ] memberFieldsToBindTo = { "name" , "electronic mail" , "phoneNo" , "country" } ; strategy. setColumnMapping (memberFieldsToBindTo) ; CsvToBean < MyUser > csvToBean = new CsvToBeanBuilder (reader) . withMappingStrategy (strategy) . withSkipLines ( ane ) . withIgnoreLeadingWhiteSpace ( truthful ) . build ( ) ; Iterator < MyUser > myUserIterator = csvToBean. iterator ( ) ; while (myUserIterator. hasNext ( ) ) { MyUser myUser = myUserIterator. adjacent ( ) ; System .out. println ( "Name : " + myUser. getName ( ) ) ; Arrangement .out. println ( "Email : " + myUser. getEmail ( ) ) ; Organization .out. println ( "PhoneNo : " + myUser. getPhoneNo ( ) ) ; System .out. println ( "Country : " + myUser. getCountry ( ) ) ; System .out. println ( "---------------------------" ) ; } } } }
The ColumnPositionMappingStrategy
is used to declare position based mapping. In the above case, we accept leap the commencement column to name
field, the 2nd column to email
field so on…
Generating a CSV file
You can generate a CSV file either from an array of Strings or from a Listing of objects.
Generate CSV file from Assortment of Strings
The example beneath shows how to generate a CSV file past writing an Array of Strings into each row of the CSV file.
import com.opencsv. CSVWriter ; import java.io. Author ; import coffee.nio.file. Files ; import coffee.nio.file. Paths ; import coffee.io. IOException ; public class OpenCSVWriter { individual static last String STRING_ARRAY_SAMPLE = "./string-assortment-sample.csv" ; public static void main ( String [ ] args) throws IOException { endeavour ( Writer writer = Files . newBufferedWriter ( Paths . get (STRING_ARRAY_SAMPLE) ) ; CSVWriter csvWriter = new CSVWriter (writer, CSVWriter .DEFAULT_SEPARATOR, CSVWriter .NO_QUOTE_CHARACTER, CSVWriter .DEFAULT_ESCAPE_CHARACTER, CSVWriter .DEFAULT_LINE_END) ; ) { String [ ] headerRecord = { "Proper name" , "E-mail" , "Telephone" , "Country" } ; csvWriter. writeNext (headerRecord) ; csvWriter. writeNext ( new Cord [ ] { "Sundar Pichai ♥" , "sundar.pichai@gmail.com" , "+1-1111111111" , "India" } ) ; csvWriter. writeNext ( new String [ ] { "Satya Nadella" , "satya.nadella@outlook.com" , "+ane-1111111112" , "Republic of india" } ) ; } } }
Generate CSV file from Listing of Objects
Finally, following is an example showing how to generate a CSV file from Listing of objects. The instance uses the MyUser
class defined in the previous department -
import com.opencsv. CSVWriter ; import com.opencsv.bean. StatefulBeanToCsv ; import com.opencsv.edible bean. StatefulBeanToCsvBuilder ; import com.opencsv.exceptions. CsvDataTypeMismatchException ; import com.opencsv.exceptions. CsvRequiredFieldEmptyException ; import java.io. IOException ; import java.io. Writer ; import java.nio.file. Files ; import java.nio.file. Paths ; import java.util. ArrayList ; import java.util. Listing ; public form OpenCSVWriter { private static terminal String OBJECT_LIST_SAMPLE = "./object-list-sample.csv" ; public static void primary ( String [ ] args) throws IOException , CsvDataTypeMismatchException , CsvRequiredFieldEmptyException { attempt ( Author writer = Files . newBufferedWriter ( Paths . get (STRING_ARRAY_SAMPLE) ) ; ) { StatefulBeanToCsv < MyUser > beanToCsv = new StatefulBeanToCsvBuilder (writer) . withQuotechar ( CSVWriter .NO_QUOTE_CHARACTER) . build ( ) ; List < MyUser > myUsers = new ArrayList < > ( ) ; myUsers. add ( new MyUser ( "Sundar Pichai ♥" , "sundar.pichai@gmail.com" , "+one-1111111111" , "Republic of india" ) ) ; myUsers. add together ( new MyUser ( "Satya Nadella" , "satya.nadella@outlook.com" , "+one-1111111112" , "India" ) ) ; beanToCsv. write (myUsers) ; } } }
Conclusion
That's all folks! In this commodity, We looked at different means of reading and writing CSV files in Java using OpenCSV library.
You tin can discover all the code samples presented in this article in my github repository. Consider giving the repository a star on github if y'all find it useful.
Thank y'all for reading. Encounter yous in the next mail.
Source: https://www.callicoder.com/java-read-write-csv-file-opencsv/
Post a Comment for "How to Use Java to Read All Csv Files in a Folder"