Question

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!

Was it helpful?

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.

OTHER TIPS

If you were willing to switch open source APIs to Super CSV (which distinguishes between the physical line and the CSV row), then you will have the following 3 methods available to you:

/**
 * Gets the current position in the file. 
 * The first line of the file is line number 1.
 * 
 * @since 1.0
 */
int getLineNumber();

/**
 * Returns the untokenized CSV row that was just read 
 * (which can potentially span multiple lines in the file).
 * 
 * @return the untokenized CSV row that was just read
 * @since 2.0.0
 */
String getUntokenizedRow();

/**
 * Gets the current row number (i.e. the number of CSV records - including the 
 * header - that have been read). This differs from the lineNumber, which is 
 * the number of real lines that have been read in the file. 
 * The first row is row 1 (which is typically the header row).
 * 
 * @since 2.0.0
 */
int getRowNumber();

You write that you need the line number for error reporting.
The CsvException class has a getLineNumber method that you can use.

Of course, this only works when you have an Exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top