Let’s see how to read CSV file in Java and map the records to Java object using Open CSV library.

How to read CSV file in Java:

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.

Maven dependency:

Add the below maven dependency in your application’s pom.xml file

pom.xml
<dependency>
  <groupId>com.opencsv</groupId>
  <artifactId>opencsv</artifactId>
  <version>4.1</version>
</dependency>

Sample CSV file:

data.csv
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.

CSV_Object.java
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.

CSVParser.java
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)

References:

Happy Learning 🙂

About the Author:

Avatar
Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment