如果我尝试读取名为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*/
}
答案 0 :(得分:1)
通常,使用InputStreamReader(InputStream in)
构造函数是不好的做法,它使用“默认字符集”。您应该明确指定charset。
但这很难解释你的问题。
答案 1 :(得分:1)
我的系统没有区别:
输出:
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();
}
}
答案 2 :(得分:0)
嗯,Java会忽略文件的文件扩展名(实际上,通常唯一关心文件扩展名的是Windows)。我猜你会遇到某种新线/非常微妙的编码问题导致你看到的问题。
答案 3 :(得分:0)
您的编辑在保存文件时是否做了一些特别的事情?你在Windows上工作吗? (Linux和Windows之间存在一些Newline差异,尽管我从来没有遇到过使用Java的麻烦)