简体   繁体   English

如何使用java中的CsvReader从csv文件中读取特定行

[英]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. 我正在使用CsvReader库,并希望从java中的csv文件中读取特定的行。 Sample csv : 示例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'. 我想读取名称为'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. 由于行具有不同的字节大小,并且由于CSV格式不包含索引,因此您无法直接对一行进行随机访问

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/ 我对这种类型的操作有一些经验,只需尝试这个API http://opencsv.sourceforge.net/

we have an option to skip the first n records 我们可以跳过前n条记录

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

this shows it will skip the first 2 records. 这表明它将跳过前2个记录。 look through it 透过它看

Other API's 其他API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在CsvReader中使用getHeaders()方法读取csv文件的标头值 - How to read header values of a csv file using getHeaders() method in CsvReader 使用csvreader在Java中无法正确读取CSV - CSV is not getting read properly in java using csvreader 如何从 java 中的 csv 文件中读取行的特定列 - How to read a specific column of a row from a csv file in java 如何使用jsp / java从csv文件的特定列读取数据? - How to read data from a specific column from csv file using jsp/java? Java CSVReader会跳过行以及如何转换csv - Java CSVReader skips rows & how to transform csv 如何使用搜索字符串从 CSV 文件中删除特定行 - How to delete a specific row from a CSV file using search string csvReader正在从csv文件读取时跳过第一列每一行的第一个字母 - csvReader is skipping the first alphabet of every row of fist column while reading from a csv file 使用openCSV CSVReader读取csv文件时出现Java应用程序错误... java.lang.ArrayIndexOutOfBoundsException - Java application Error while reading a csv file using openCSV CSVReader … java.lang.ArrayIndexOutOfBoundsException 如何从 csv 文件中读取特定范围并写入 Java 中的另一个 csv 文件? - How to read specific range from a csv file and write to another csv file in Java? 如何替换CSV文件中的特定行? - How to replace a specific row from a CSV file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM