2

如果我尝试读取名为 .csv 的 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

一般来说,使用InputStreamReader(InputStream in)使用“默认字符集”的构造函数是不好的做法。您应该明确指定字符集。

不过,这很难解释你的问题。

于 2011-01-03T14:47:50.353 回答
1

我的系统没有区别:

  • Windows Vista SP2(32 位)
  • NTFS
  • JDK 1.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 回答
0

好吧,Java会忽略文件的文件扩展名(实际上通常唯一关心文件扩展名的是Windows)。我猜你有某种换行/非常微妙的编码问题导致你看到的问题。

于 2011-01-03T14:11:16.477 回答
0

您的编辑器在保存文件时是否做了一些特别的事情?你在 Windows 上工作吗?(linux和windows之间有一些换行符的区别,虽然我在使用Java时从来没有遇到过麻烦)

于 2011-01-03T14:12:44.263 回答