csvline-numbersopencsv

OpenCSV - Find out line number


How do I know the actual line number on file associated to a line read by the CSVReader class? I can count the lines assuming every line read by this class is a new line on file. The problem is that it is possible to have newline characters in the CSV file. So for example, having 3 "logical" lines does not mean we have 3 "physical" lines on file. I have an error reporting feature that almost always report the wrong line number because of this.

Any ideas how to determine the real line number on file? Thanks!


Solution

  • If you are willing to modify the source code, you can add a counter to

    private String getNextLine()
    

    Increment the counter in the two places where

    br.readLine();
    

    is called, and expose the counter as a public property.

    If you do not wish to modify the source code, for each CSV line returned, you could increment your own counter by 1 + the sum of the newline characters in the CSV line (presumably OpenCSV is returning columns including the newline characters to your code, though I have not tested that behavior). If you had Column A with one newline and Column B with two newlines, the actual file should look something like:

    "This is

    Cell A","And

    Cell

    B"

    resulting in 3 newline characters (or \r\n sequences, depending on your platform), plus the 1 line returned by OpenCSV. Increment your counter by 4.