Java - How to read CSV file and Map to Java Object - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Let’s see how to read CSV file in Java and map the records to Java object using Open CSV library.
Open CSV is an easy to use CSV (comma-separated value) library for java users, using this library we can easily convert CSV row into an Object.
Add the below maven dependency in your application’s pom.xml file
<dependency>
  <groupId>com.opencsv</groupId>
  <artifactId>opencsv</artifactId>
  <version>4.1</version>
</dependency>
hotel_name,distance,review_score,Price,Scraping_time,Data_rank,Page_number,CheckInDate,CheckOutDate
Artim Hotel,1.8 miles from centre,7.7,€ 81,2020-03-25 10:41:23,2,1,Wednesday 25 March 2020,Thursday 26 March 2020
Hotel Cityblick,3.1 miles from centre,5.9,€ 34,2020-03-25 10:41:23,4,1,Wednesday 25 March 2020,Thursday 26 March 2020
annabanana Hostel,2.5 miles from centre,8.1,€ 50,2020-03-25 10:41:23,6,1,Wednesday 25 March 2020,Thursday 26 March 2020
Prepare a Java class to map the above CSV data.
public class CSV_Object{
    private String hotelName;
    private String distance;
    private String reviewScore;
    private String price;
    private String scrapingTime;
    private String dataRank;
    private String pageNumber;
    private String checkinDate;
    private String ceckoutDate;
    //setters/getters..
}

Now let’s parse the above CSV file using open CSV library.
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
public class CSVParser {

    public void parse(){
        try{
            CSVReader reader=
                    new CSVReaderBuilder(new FileReader("C:/cgoka/data.csv")).
                            withSkipLines(1). // Skiping firstline as it is header
                            build();
            List<CSV_Object> csv_objectList=reader.readAll().stream().map(data-> {
                CSV_Object csvObject= new CSV_Object();
                csvObject.setHotelName(data[0]);
                csvObject.setDistance(data[1]);
                csvObject.setReviewScore(data[2]);
                csvObject.setPrice(data[3]);
                csvObject.setScrapingTime(data[4]);
                csvObject.setDataRank(data[5]);
                csvObject.setPageNumber(data[6]);
                csvObject.setCheckinDate(data[7]);
                csvObject.setCeckoutDate(data[8]);
                return csvObject;
            }).collect(Collectors.toList());
            csv_objectList.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output:
CSV_Object(hotelName=Artim Hotel, distance=1.8 miles from centre, reviewScore=7.7, price=€ 81, scrapingTime=2020-03-25 10:41:23, dataRank=2, pageNumber=1, checkinDate=Wednesday 25 March 2020, ceckoutDate=Thursday 26 March 2020)
CSV_Object(hotelName=Hotel Cityblick, distance=3.1 miles from centre, reviewScore=5.9, price=€ 34, scrapingTime=2020-03-25 10:41:23, dataRank=4, pageNumber=1, checkinDate=Wednesday 25 March 2020, ceckoutDate=Thursday 26 March 2020)
CSV_Object(hotelName=annabanana Hostel, distance=2.5 miles from centre, reviewScore=8.1, price=€ 50, scrapingTime=2020-03-25 10:41:23, dataRank=6, pageNumber=1, checkinDate=Wednesday 25 March 2020, ceckoutDate=Thursday 26 March 2020)

Open CSV
Read CSV file using Python.
Happy Learning 🙂

How to Read CSV File in Python
Shell Script How to write logs in a Separate File
Java 8 Read File Line By Line Example
4 ways to create an Object in Java
JAXB XML to Java Object Conversion Example
JAXB Java Object to XML Conversion Example
Java How to create Jar File ?
Writing data to a file in Java
How to Search a file in a Directory using Java
How to convert Java Object to JSON
How to convert JSON to Java Object Example
How to convert JSON to Java Map Object
count number of lines characters and words in a file Java
Java 8 walk How to Read all files in a folder
Different types of Object States in Hibernate

How to Read CSV File in Python
Shell Script How to write logs in a Separate File
Java 8 Read File Line By Line Example
4 ways to create an Object in Java
JAXB XML to Java Object Conversion Example
JAXB Java Object to XML Conversion Example
Java How to create Jar File ?
Writing data to a file in Java
How to Search a file in a Directory using Java
How to convert Java Object to JSON
How to convert JSON to Java Object Example
How to convert JSON to Java Map Object
count number of lines characters and words in a file Java
Java 8 walk How to Read all files in a folder
Different types of Object States in Hibernate
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String