Artwar
Artwar

Reputation: 11

Skip Optional Header in CSV file with SuperCSV

I have a system where users can import data through csv files. The file is parsed to POJOs using SuperCSV.

I want to be able to manage these files with or without the header row.

CsvBeanReader.getHeader(true) will extract the first row, but if it is not a header then that row won't be parsed by the BeanReader.

Is there an easy way of detecting the header without losing the first line of data?

Upvotes: 1

Views: 2640

Answers (3)

Dinakar Prasad Maurya
Dinakar Prasad Maurya

Reputation: 371

The best idea to skip header from super csv while reading the values from file:-

//dont write this line beanReader.getHeader(true)

// before reading the values pass headers array that u have declared like headers = {column1},{column2}

//then use below lines, which exclude the header

beanReader.read(type, headers, processors))

Above steps help us to read the csv file which was saved without header.

Upvotes: 1

Artwar
Artwar

Reputation: 11

I found a solution.

By wrapping the provided Reader using a BufferedReader I could mark the beginning, read the first line, and then do reader.reset() if it's not the header.

Upvotes: 0

James Bassett
James Bassett

Reputation: 9868

The easiest solution would be to just read the file twice (check for expected headers on the first pass - you only need to read the first line, and read with/without header on the second pass).

Upvotes: 0

Related Questions