1

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.

Charles
  • 50,943
  • 13
  • 104
  • 142
prasun
  • 63
  • 8

2 Answers2

4

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.

rodion
  • 14,729
  • 3
  • 53
  • 55
0

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.");
    }
}
Butiri Dan
  • 1,759
  • 5
  • 12
  • 18
Yaniv Levy
  • 78
  • 10