我如何知道文件中与CSVReader类读取的行相关联的实际行号?我可以计算行数,假设这个类读取的每一行都是文件中的新行。问题是CSV文件中可能包含换行符。因此,例如,拥有3行“逻辑”行并不意味着我们在文件中有3行“物理”行。我有一个错误报告功能,几乎总是因为这个而报告错误的行号。
你知道如何确定文件中的真实行号吗?谢谢!
发布于 2012-09-11 02:15:00
如果您希望修改source code,可以将计数器添加到
private String getNextLine()在以下两个位置递增计数器
br.readLine();并将计数器作为公共属性公开。
如果您不希望修改源代码,对于返回的每个CSV行,您可以将您自己的计数器递增1 + the sum of the newline characters in the CSV line (假设OpenCSV会将包含换行符的列返回到您的代码中,尽管我还没有测试过这种行为)。如果A列有一个换行符,B列有两个换行符,那么实际的文件应该如下所示:
“这是
单元格A“、”和
单元格
B“
产生3个换行符(或\r\n序列,取决于您的平台),加上OpenCSV返回的1行。将计数器递增4。
发布于 2012-12-13 05:44:38
如果您愿意将开源API切换到Super CSV (区分物理行和CSV行),那么您可以使用以下3种方法:
/**
* 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();发布于 2019-12-02 20:38:05
您写道,您需要用于错误报告的行号。
CsvException类有一个您可以使用的getLineNumber方法。
当然,只有当你有一个异常时,这才能起作用。
https://stackoverflow.com/questions/12357163
复制相似问题