1

CSVReader クラスによって読み取られた行に関連付けられているファイルの実際の行番号を知るにはどうすればよいですか? このクラスによって読み取られるすべての行がファイルの新しい行であると仮定して、行を数えることができます。問題は、CSV ファイルに改行文字が含まれている可能性があることです。したがって、たとえば、3 つの「論理」行があるからといって、ファイルに 3 つの「物理」行があるとは限りません。このため、ほとんどの場合、間違った行番号を報告するエラー報告機能があります。

ファイルの実際の行番号を特定する方法はありますか? ありがとう!

4

3 に答える 3

0

オープン ソース 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();
于 2012-12-12T21:44:37.690 に答える
0

ソース コードを変更する場合は、カウンターを追加できます。

private String getNextLine()

2 つの場所でカウンターをインクリメントします。

br.readLine();

が呼び出され、カウンターをパブリック プロパティとして公開します。

ソース コードを変更したくない場合は、返された CSV 行ごとに、独自のカウンターをインクリメントできます1 + the sum of the newline characters in the CSV line(おそらく、OpenCSV は改行文字を含む列をコードに返しますが、その動作はテストしていません)。列 A に改行が 1 つ、列 B に改行が 2 つある場合、実際のファイルは次のようになります。

"これは

セルA」、「そして

細胞

B"

3 つの改行文字 (プラットフォームによっては \r\n シーケンス) と、OpenCSV によって返される 1 行になります。カウンターを 4 増やします。

于 2012-09-10T18:15:00.143 に答える