Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get CSV file header using apache commons

I have been looking for the past 2 hours for a solution to my problem in vain. I'am trying to read a CSV File using Apache commons ,I am able to read the whole file but my problem is how to extract only the header of the CSV in an array?

like image 601
user3382344 Avatar asked Mar 28 '16 19:03

user3382344


People also ask

How does Apache Commons read CSV files?

csv . It's very easy to read such CSV files with Apache Commons CSV. You just need to add a single setting called withFirstRecordAsHeader() . Apache Commons CSV uses the first record as the header record and allows you to retrieve the values using the header names.

What is Apache Commons CSV?

Using Apache Commons CSV Commons CSV reads and writes files in variations of the Comma Separated Value (CSV) format. The most common CSV formats are predefined in the CSVFormat class: Microsoft Excel.

Can we read CSV file with Apache POI?

Apache POI was never designed to call on CSV files. While a CSV File may be opened in Excel, Excel has its own reader that does an auto import. This is assuming that your CSV has the . csv instead of the .

What is CSVFormat rfc4180?

CSV is a simple format for representing a rectangular array (matrix) of numeric and textual values. It an example of a "flat file" format. It is a delimited data format that has fields/columns separated by the comma character %x2C (Hex 2C) and records/rows/lines separated by characters indicating a line break.


1 Answers

In Kotlin:

val reader = File(path).bufferedReader()
val records = CSVFormat.DEFAULT.withFirstRecordAsHeader()
    .withIgnoreHeaderCase()
    .withTrim()
    .parse(reader)

println(records.headerNames)
like image 192
Mitchell Tracy Avatar answered Sep 23 '22 18:09

Mitchell Tracy