如何知道與CSVReader類讀取的行關聯的文件上的實際行號?我可以計算這些行,假定這個類讀取的每一行都是一個新的文件行。問題是CSV文件中可能有換行符。因此,例如,擁有3個「邏輯」行並不意味着我們有3個「物理」行文件。我有一個錯誤報告功能,因此幾乎總是報告錯誤的行號。OpenCSV - 找出行號碼
任何想法如何確定文件上的實際行號?謝謝!
如何知道與CSVReader類讀取的行關聯的文件上的實際行號?我可以計算這些行,假定這個類讀取的每一行都是一個新的文件行。問題是CSV文件中可能有換行符。因此,例如,擁有3個「邏輯」行並不意味着我們有3個「物理」行文件。我有一個錯誤報告功能,因此幾乎總是報告錯誤的行號。OpenCSV - 找出行號碼
任何想法如何確定文件上的實際行號?謝謝!
如果你願意修改source code,你可以在櫃檯在兩個地方
br.readLine();
被稱爲添加到
private String getNextLine()
增加計數器,並露出計數器作爲公共財產。
如果您不希望修改源代碼,對於每個返回的CSV行,您可以通過1 + the sum of the newline characters in the CSV line
增加自己的計數器(假設OpenCSV將包括換行符的列返回給您的代碼,但我沒有測試過這種行爲)。如果你有兩個新行了A柱與一個新行和列B,實際的文件應該是這個樣子:
「這是
小區A」,「而且
細胞
B「
產生3個換行字符(或\ r \ n序列,取決於您的平臺)以及OpenCSV返回的1行。通過4
增加你方如果願意開源的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();
我試圖避免這些事情,但我想這是不可能的,所以我會採取第二種選擇。謝謝。 – Paul