Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read a specific row from csv file using CsvReader in java

Tags:

java

csv

I am using CsvReader library and want to read a specific row from a csv file in java. Sample csv :

**Name**,     **Address**,        **Email-Id**
student,    studentaddress,     [email protected] 
student2,   student2address,    [email protected] 
employee,   employeeaddres1,    [email protected]

I want to read the row where name is 'student2'. Could you please provide a solution?

Thanks in advance.

like image 743
Doxyuser Avatar asked Mar 26 '26 06:03

Doxyuser


2 Answers

As rows have different sizes in bytes, and as the CSV format doesn't contain an index, you can't have a random access directly to one row.

So you must read all precedent rows and simply skip them until you're at the desired one.

like image 146
Denys Séguret Avatar answered Mar 28 '26 20:03

Denys Séguret


I have some experience with this type of operation , just try this API http://opencsv.sourceforge.net/

we have an option to skip the first n records

eg: CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t', '\'', 2);

this shows it will skip the first 2 records. look through it

Other API's

like image 44
Abin Manathoor Devasia Avatar answered Mar 28 '26 20:03

Abin Manathoor Devasia