prasun
prasun

Reputation: 63

Read Specific line using SuperCSV

Is it possible to read a specific line using SuperCsv?

Suppose a .csv file contains 100 lines and i want to read line number 11.

Upvotes: 1

Views: 1622

Answers (2)

Yaniv Levy
Yaniv Levy

Reputation: 78

Here is a simple solution which you can adapt:

listReader = new CsvListReader(new InputStreamReader(new FileInputStream(CSVFILE, CHARSET), CsvPreference.TAB_PREFERENCE);
listReader.getHeader(false);
while ((listReader.read(processors)) != null) {
    if (listReader.getLineNumber() == 1) {
        System.out.println("Do whaever you need.");
    }
}

Upvotes: 0

rodion
rodion

Reputation: 15029

CSV files usually contain variable-length records, which means it is impossible to "jump" to a specified record. The only solution is to sequentially read CSV records from the beginning of the file, while keeping a count, until you reach the needed record.

I have not found any special API in SuperCsv for doing this skipping of lines, so I guess you will have to manually call CsvListReader#read() method 11 times to get the line you want.

I don't know if other CSV reading libraries will have a "jump-to-line" feature, and even if they do, it is unlikely to perform any better than manually skipping to the required line, for the reason given in the first paragraph.

Upvotes: 4

Related Questions