简体   繁体   English

Java BufferedFileWriter只写入50%的输入行

[英]Java BufferedFileWriter writes only 50% of input lines

My TAB-delimited input file has 1 million lines , it looks like this: 我的TAB分隔输入文件有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 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

I've coded a file format transformation, and the output file to looks like this: 我编写了一个文件格式转换,输出文件如下所示:

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

I have two problems : 我有两个问题

  • the output file only has 50% of input file lines 输出文件只有50%的输入文件行
  • the output file has missing lines, eg t2 's line is missing 输出文件缺少行,例如缺少t2的行

Here's my code, thanks in advance! 这是我的代码,提前谢谢!

Note: I've also added a buffer size to new BufferedWriter()/Reader() , no impact. 注意:我还为new BufferedWriter()/Reader()添加了缓冲区大小,没有任何影响。

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

the output file only has 8.3% of input file lines 输出文件只有8.3%的输入文件行

As far as you code is concerned, It should be 50% of the lines should be missing. 就你的代码而言,它应该是50%的行应该丢失。 You have the difference in size because the data that is in the parent file is of different format than that in the file you are creating. 您的大小不同,因为父文件中的数据格式与您正在创建的文件格式不同。 I am saying this because you code skips the alternate lines. 我之所以这样说,是因为您的代码会跳过备用行。

Let me explain, in your while loop condition you are using line = br.readLine() Which reads the line 1. now in the first line of the while loop you are again using line = br.readLine() this will read the line 2. the file. 让我解释一下,在你的while循环条件中你使用的是line = br.readLine()它读取了行1.现在在while循环的第一行你再次使用line = br.readLine()这将读取该行2.文件。 You are using it to write the data, so line 2 data gets written. 您正在使用它来写入数据,因此第2行数据被写入。 Now in the second looping, in the while loop condition you are reading line 3 of the file and in the first line of while loop you are reading line 4 of the file and this line gets written. 现在在第二个循环中,在while循环条件下,您正在读取文件的第3行,在while循环的第一行中,您正在读取文件的第4行,并且此行被写入。 So you see you get 50% of the output. 所以你看到你获得了50%的输出。

Now you think you understand why you are getting lesser lines in the output file. 现在你认为你理解为什么你在输出文件中得到较少的行。 so the simple solution is to get rid of preferable the first line of the while loop and let the condition remain the same. 所以简单的解决方案是摆脱while循环的第一行,并让条件保持不变。

this behavior can be attributed to the following two lines in the code. 此行为可归因于代码中的以下两行。

while ((line = br.readLine()) != null) {
                // READING
                line = br.readLine();

you are reading two lines from the file one during while check and one during the line = br.readline() , causing skipped lines. 你在检查期间从文件中读取两行,在行= br.readline()期间读取一行,导致跳过的行。 you should read only at the while loop check. 你应该只在while循环检查时阅读。

   while ((line = br.readLine()) != null) {
     // use line variable value for printing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM