OpenCSV - Find out line number

opencsv custom converter
opencsv github
csvreader
csv reader java
java csv library

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!

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.

opencsv / Support Requests / #72 Obtainng input line number using , I need a way to obtain the line number of the text file where a bean is read. I can't figure how to do it. Do you have any suggest?</t>. Best regards� Because opencsv is multithreaded by default, the getCapturedExceptions() method in CsvToBean and StatefulBeanToCsv can potentially return more exceptions than expected if a handler is used that collects a certain number of exceptions before throwing one; any threads still active when another thread throws an exception are allowed to finish.

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();

opencsv / Feature Requests / #73 Keep track of and return actual , For user feedback it would be great to determine the actual line number where the CSVReader is currently positioned (e.g. during� Bug 174 - The line number reported in exceptions thrown was actually a record number. That is wrong from the perspective of the expectation based on the variable name (lineNumber), but it is also very unhelpful from the perspective of debugging, when the true line number from the file is necessary to find bad data.

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.

opencsv / Bugs / #174 Wrong line number in parsing error, The line number I got from the error message was one below the actual time I read the file with opencsv, I get an error with a different line number, randomly. Would it be possible for you to attach your data and your code? OpenCSV is more popular because we don’t have any builtin CSV parser in java. Some of the important classes in OpenCSV CSV parser are; CSVReader: This is the most important class in OpenCSV. CSVReader class is used to parse CSV files. We can parse CSV data line by line or read all data at once.

CSVReader (opencsv 5.2 API), The default limit for the number of lines in a multiline record. Returns if the reader will keep carriage returns found in data or remove them. If verifyReader () was set to true certain IOException s will still be passed out as they are indicative of� OpenCSV is able to serialize .csv files into preset and reusable schemas implemented as annotated Java pojo beans. CsvToBean is constructed using CsvToBeanBuilder. As of OpenCSV 4, CsvToBeanBuilder is the recommended way to work with com.opencsv.bean.CsvToBean. Here's a simple bean we can use to serialize our two-column.csv from section 2.:

opencsv –, Arbitrary numbers of values per line. Here is an overview of how to use opencsv in your project. Flow of data through opencsv Most users of opencsv find themselves needing to read CSV files, and opencsv excels at� Each line in a CSV file is a data record. Each record consists of one or more fields, separated by commas. Despite CSV format being a very simple format, there can be many differecies, such as different delimiters, new lines, or quoting characters.

ProcessCsvLine (opencsv 5.2 API), The only constructor for creating a bean out of a line of input. Parameters: lineNumber - Which record in the input file is being processed: mapper - The mapping� Read a CSV file and parse the records into a Java object without using annotations. If you don’t want to clutter your POJO class with OpenCSV annotations, then you can use Mapping strategies to specify the mapping between CSV columns and object member fields.

Comments
  • I was trying to avoid these things, but I guess it's not possible, so I'll take the second option. Thanks.