Java BufferedFileWriter只写入50%的输入行
我的制表符分隔的输入文件有100万行,如下所示:Java BufferedFileWriter只写入50%的输入行,java,file-io,Java,File Io,我的制表符分隔的输入文件有100万行,如下所示: id name artist_name genre notoriete_fr notoriete_us notoriete_uk notoriete_it notoriete_sp notoriete_no notoriete_de notoriete_wd 1 10ème bougie 113 rap 0 -5 -5 -5 -5 -5 -5 -5 2 I'm n
id name artist_name genre notoriete_fr notoriete_us notoriete_uk notoriete_it notoriete_sp notoriete_no notoriete_de notoriete_wd
1 10ème bougie 113 rap 0 -5 -5 -5 -5 -5 -5 -5
2 I'm not in love 10cc pop 1 1 1 1 1 1 1 1
5 Generation Black Rebel Motorcycle Club rock 0 0 0 0 0 0 0 0
id:ID;genre;notoriete_fr:int;notoriete_us:int;notoriete_uk:int;notoriete_sp:int;notoriete_de:int;notoriete_it:int;notoriete_no:int;notoriete_wd:int;:LABEL
t1;rap;0;-5;-5;-5;-5;-5;-5;-5;Track
t5;rock;0;0;0;0;0;0;0;0;Track
我编写了一个文件格式转换,输出文件如下所示:
id name artist_name genre notoriete_fr notoriete_us notoriete_uk notoriete_it notoriete_sp notoriete_no notoriete_de notoriete_wd
1 10ème bougie 113 rap 0 -5 -5 -5 -5 -5 -5 -5
2 I'm not in love 10cc pop 1 1 1 1 1 1 1 1
5 Generation Black Rebel Motorcycle Club rock 0 0 0 0 0 0 0 0
id:ID;genre;notoriete_fr:int;notoriete_us:int;notoriete_uk:int;notoriete_sp:int;notoriete_de:int;notoriete_it:int;notoriete_no:int;notoriete_wd:int;:LABEL
t1;rap;0;-5;-5;-5;-5;-5;-5;-5;Track
t5;rock;0;0;0;0;0;0;0;0;Track
我有两个问题:
- 输出文件只有50%的输入文件行
- 输出文件缺少行,例如,
的行丢失t2
public static void main(String[] args) throws Exception {
BufferedReader br = null;
BufferedWriter bw = null;
try{
// prepare input file
File inFile = new File(inputFile);
br = new BufferedReader(new FileReader(inFile));
String line = "";
String cvsSplitBy = "\t";
// prepare output file
File outFile = new File(outputFile);
bw = new BufferedWriter(new FileWriter(outFile));
// Write header
bw.write("id:ID;genre;notoriete_fr:int;notoriete_us:int;notoriete_uk:int;notoriete_sp:int;notoriete_de:int;notoriete_it:int;notoriete_no:int;notoriete_wd:int;:LABEL\n");
while ((line = br.readLine()) != null) {
// READING
line = br.readLine();
String[] features = line.split(cvsSplitBy);
// WRITING
bw.write("t"+features[0]+";"+features[3]+";"+features[4]+";"+features[5]+";"+features[6]+";"+features[7]+";"+features[8]+";"+features[9]+";"+features[10]+";"+features[11]+";Track\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
此行为可归因于代码中的以下两行
while ((line = br.readLine()) != null) {
// READING
line = br.readLine();
您正在从文件中读取两行,一行在while check期间读取,另一行在line=br.readline()期间读取,导致跳过行。您应该在while循环检查时只读
while ((line = br.readLine()) != null) {
// use line variable value for printing
输出文件只有8.3%的输入文件行
就您的代码而言,应该有50%的行丢失。由于父文件中的数据与正在创建的文件中的数据格式不同,因此大小不同。我这样说是因为你的代码跳过了替换行
让我解释一下,在while循环条件下,您使用的是读取第1行的line=br.readLine()
。现在,在while循环的第一行中,您再次使用line=br.readLine()
这将读取第2行。档案。您正在使用它来写入数据,因此第2行数据被写入。现在在第二个循环中,在while循环条件下,您正在读取文件的第3行,在while循环的第一行中,您正在读取文件的第4行,这一行被写入。所以你看到你得到了50%的产出
现在,您认为您理解了为什么输出文件中的行数较少。所以简单的解决方案是去掉while循环的第一行,让条件保持不变 取出
line=br.readLine()代码>。当((line=br.readLine())!=null)
时,您已经在阅读中的行了。关于代码质量的旁注:除非您愿意将代码减少到最低限度,否则您应该使用更多的方法。意思:一部分是写输出文件;这里的另一部分是“处理”输入以生成修改的行。这些东西是真正独立的;他们不应该用同样的方法拥抱在一起。aarrgg!!!谢谢大家,@Jägermeister,虽然我不是开发人员,但我正在考虑你们的最佳实践建议,谢谢@古祖92欢迎您;万一你想知道“为什么这样更好”;我可以全心全意地推荐你看看这本书:。。。每一页都值得一读你是对的!!我被另一个文件测试弄糊涂了,它确实丢失了50%,谢谢!