2

というCSVファイルを読み込もうとすると、csv_file.csv. 問題は、行を読むとBufferedReader.readLine()最初の行が月単位でスキップされることです。しかし、ファイルの名前を変更するcsv_file.txtと、問題なく読み取れ、最初の行がスキップされません。

私が気付いていない BufferedReader の文書化されていない「機能」はありますか?

ファイルの例:

Months, SEP2010, OCT2010, NOV2010
col1, col2, col3, col4, col5
aaa,,sdf,"12,456",bla bla bla, xsaffadfafda
and so on, and so on, "10,00", xxx, xxx

コード:

FileInputStream stream = new FileInputStream(UploadSupport.TEMPORARY_FILES_PATH+fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
String line = br.readLine();
String months[] = line.split(",");
while ((line=br.readLine())!=null) {
    /*parse other lines*/
}
4

4 に答える 4

1

私のシステムでは違いはありません:

  • Windows Vista SP2 (32 ビット)
  • NTFS
  • JDK1.6.0_17

出力:

Creating C:\workspace\Sandbox\src\data.txt

Reading C:\workspace\Sandbox\src\data.txt
Skipped: 'Months, SEP2010, OCT2010, NOV2010'
First read: 'col1, col2, col3, col4, col5'

Reading C:\workspace\Sandbox\src\data.csv
Skipped: 'Months, SEP2010, OCT2010, NOV2010'
First read: 'col1, col2, col3, col4, col5'

Reading C:\workspace\Sandbox\src\data.txt
Skipped: 'Months, SEP2010, OCT2010, NOV2010'
First read: 'col1, col2, col3, col4, col5'

コード:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;


public class BuffReadTest {

    public static void main(final String[] args) {
        final String baseFilename = args[0] + "/data";
        try {
            final File txtFile = new File(baseFilename+".txt");
            final File csvFile = new File(baseFilename+".csv");

            if (txtFile.exists())   txtFile.delete();
            if (csvFile.exists())   csvFile.delete();
            createFile(txtFile.getAbsolutePath());

            readFile(txtFile.getAbsolutePath());

            txtFile.renameTo(csvFile);
            readFile(csvFile.getAbsolutePath());

            csvFile.renameTo(txtFile);
            readFile(txtFile.getAbsolutePath());

        } catch (final IOException ex) {
            System.out.println("Exception: "+ex);
            ex.printStackTrace();
        }
    }

    private static void createFile(final String filename)
            throws FileNotFoundException {
        System.out.println("\nCreating "+filename);
        final PrintWriter pw = new PrintWriter(filename);
        pw.println("Months, SEP2010, OCT2010, NOV2010");
        pw.println("col1, col2, col3, col4, col5");
        pw.println("aaa,,sdf,\"12,456\",bla bla bla, xsaffadfafda");
        pw.println("and so on, and so on, \"10,00\", xxx, xxx");
        pw.close();
    }

    private static void readFile(final String filename)
            throws FileNotFoundException, IOException {
        System.out.println("\nReading "+filename);
        final FileInputStream stream = new FileInputStream(filename);
        final BufferedReader br = new BufferedReader(new InputStreamReader(stream));
        final String skipped = br.readLine();
        final String first = br.readLine();

        System.out.println("Skipped: '"+skipped+"'");
        System.out.println("First read: '"+first+"'");
        br.close();
    }

}
于 2011-01-03T15:58:54.253 に答える
1

InputStreamReader(InputStream in)一般に、「デフォルトの文字セット」を使用するコンストラクターを使用するのは悪い習慣です。文字セットを明示的に指定する必要があります。

ただし、これはあなたの問題をほとんど説明できません。

于 2011-01-03T14:47:50.353 に答える
0

ファイルのファイル拡張子は、Java では無視されます (実際、通常、ファイル拡張子を気にするのは Windows だけです)。改行/非常に微妙なコーディングの問題が原因で、問題が発生していると思います。

于 2011-01-03T14:11:16.477 に答える
0

ファイルの保存中にエディタが何か特別なことをしていますか? Windowsで作業していますか?(Linux と Windows では改行の違いがいくつかありますが、Java を使用して問題に遭遇したことはありません)

于 2011-01-03T14:12:44.263 に答える