Madhu Sudhan Baddam
Madhu Sudhan Baddam

Reputation: 11

How to read specified row in csv file

I have daily data starting from 1980 in csv file. But I want to read data only from 1985. Because the other dataset in another file starts from 1985. How can I skip reading the data before 1985 in Java?

Upvotes: 1

Views: 1425

Answers (4)

Scott Conway
Scott Conway

Reputation: 983

Madhu - if you are using openCSV then look at the javadocs for the CSVToBeanFilter. This will give you a good example of how to parse a file reading only the rows who column(s) match given criteria.

Upvotes: 0

snovelli
snovelli

Reputation: 6058

I'd suggest to use the grep command line utility to filter data instead of developing a custom solution.

grep 1985 *.csv > data_from_1985.csv

If you need a better expression to filter your data to avoid spuroius data you can use regex101.com to test it.

for example, if the lines in your CSV start with the year, you may use

grep "^1985" *.csv > data_from_1985.csv

Upvotes: 1

Jose Luis
Jose Luis

Reputation: 994

Perhaps this link can help you. It talks about the seek() function of InputStream.

You could calculate in witch byte starts the 1985 data, and skip this bytes with seek().

Upvotes: 0

read opencsv doc: http://opencsv.sourceforge.net/#how-to-read

read each line (from example):

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
 String [] nextLine;
 while ((nextLine = reader.readNext()) != null) {
    // nextLine[] is an array of values from the line
    System.out.println(nextLine[0] + nextLine[1] + "etc...");
 }

convert to int

int one_value=Integer.parseInt(nextLine[i]); // iterate i 

then, get your "year" column, and compare it to 1985

Upvotes: 1

Related Questions