CSV 与 TXT 文件中的 Java BufferedReader 行为

发布于 2024-10-09 23:50:25 字数 716 浏览 9 评论 0原文

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

If i try to read a CSV file called csv_file.csv. The problem is that when i read lines with BufferedReader.readLine() it skips the first line with months. But when i rename the file to csv_file.txt it reads it allright and it's not skipping the first line.

Is there an undocumented "feature" of BufferedReader that i'm not aware?

Example of file:

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

The code:

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*/
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落日海湾 2024-10-16 23:50:25

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

但这很难解释你的问题。

In general, it's bad practice to use the InputStreamReader(InputStream in) constructor, which uses the "default charset". You should specify the charset explicitly.

This can hardly explain to your problem, though.

怀里藏娇 2024-10-16 23:50:25

我的系统上没有区别:

  • 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();
    }

}

No difference on my system:

  • Windows Vista SP2 (32-bit)
  • NTFS
  • JDK 1.6.0_17

Output:

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'

Code:

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();
    }

}
窗影残 2024-10-16 23:50:25

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

Well, the file extension of a file is ignored by Java (actually the only thing that usually cares about file extensions is Windows). I would guess you have some sort of newline/very subtle coding issue causing the problem you see.

却一份温柔 2024-10-16 23:50:25

您的编辑器在保存文件时是否做了一些特殊的事情?您在 Windows 上工作吗? (linux 和 windows 之间有一些换行符差异,尽管我在使用 Java 时从未遇到过麻烦)

Is your editor doing something special while saving the file? Are you working on Windows? (there are some Newline differences between linux & windows, although I never ran into trouble using Java)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文