首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenCSV -查找行号

OpenCSV -查找行号
EN

Stack Overflow用户
提问于 2012-09-11 02:07:18
回答 3查看 4.7K关注 0票数 1

我如何知道文件中与CSVReader类读取的行相关联的实际行号?我可以计算行数,假设这个类读取的每一行都是文件中的新行。问题是CSV文件中可能包含换行符。因此,例如,拥有3行“逻辑”行并不意味着我们在文件中有3行“物理”行。我有一个错误报告功能,几乎总是因为这个而报告错误的行号。

你知道如何确定文件中的真实行号吗?谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-09-11 02:15:00

如果您希望修改source code,可以将计数器添加到

代码语言:javascript
复制
private String getNextLine()

在以下两个位置递增计数器

代码语言:javascript
复制
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。

票数 0
EN

Stack Overflow用户

发布于 2012-12-13 05:44:38

如果您愿意将开源API切换到Super CSV (区分物理行和CSV行),那么您可以使用以下3种方法:

代码语言:javascript
复制
/**
 * 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();
票数 0
EN

Stack Overflow用户

发布于 2019-12-02 20:38:05

您写道,您需要用于错误报告的行号。

CsvException类有一个您可以使用的getLineNumber方法。

当然,只有当你有一个异常时,这才能起作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12357163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档