CSV 与 TXT 文件中的 Java BufferedReader 行为
如果我尝试读取名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一般来说,使用
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.
我的系统上没有区别:
输出:
代码:
No difference on my system:
Output:
Code:
好吧,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.
您的编辑器在保存文件时是否做了一些特殊的事情?您在 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)