简体   繁体   中英

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

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,     student@email.com 
student2,   student2address,    student2@email.com 
employee,   employeeaddres1,    employee@email.com

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

Thanks in advance.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM