简体   繁体   English

BufferedWriter不写第一行文本

[英]BufferedWriter not Writing First Line of Text

I am merging two text files with 123456 and abcdefg on their own lines respectively. 我将两个文本文件分别与123456abcdefg合并在一起。 The phenomena I am experiencing is a file that is created that is 23456abcde on the same line, so exactly as i have it typed here. 我遇到的现象是在同一行上创建的文件为23456abcde ,因此与我在此处键入的完全相同。

My question is 我的问题是

  1. why is the 1 from the first file not being written 为什么第一个文件中的1没有被写入
  2. why are they not on their own line. 他们为什么不自己行。
  3. why is 'fg' not written. 为什么不写“ fg”。 (seven lines of text data but only six writes because seven writes will output "23456" without the alphanumerical letters at all). (七行文本数据,但是只有六次写操作,因为七次写操作将输出“ 23456”,而根本没有字母数字字母)。

.

public static String mergeRecords(String in1, String in2, String out) {
    BufferedReader br1 = null;
    BufferedReader br2 = null;
    BufferedWriter bw1 = null;

    try{
        FileReader fr1 = new FileReader(in1);
        FileReader fr2 = new FileReader(in2);
        br1 = new BufferedReader(fr1);
        br2 = new BufferedReader(fr2);
        bw1 = new BufferedWriter(new FileWriter(out));
        ////File Writing
        while(!(br1.readLine()==null)||(br2.readLine()==null)){
            //alternating between the two filles
            if((f%2)==0){
                bw1.write(br1.read());
                bw1.write(br1.readLine());
                bw1.write(br1.readLine());
                bw1.write(br1.readLine());
                bw1.write(br1.readLine());
                bw1.write(br1.readLine());
            }
            else{
                bw1.write(br2.read());
                bw1.write(br2.readLine());
                bw1.write(br2.readLine());
                bw1.write(br2.readLine());
                bw1.write(br2.readLine());
                bw1.write(br2.readLine());
            }
            /////
            //File ALternator Value
            f++;
        }
        bw1.close();
    }
    catch(IOException iox){
    }
    return "'mergeRecords' not yet implemented";
}

The problem is that: 问题是:

        while(!(br1.readLine()==null)||(br2.readLine()==null)){

consumes the first line of br1. 消耗br1的第一行。 It doesn't evaluate the second statement because the first one is already true. 它不会评估第二条语句,因为第一条已经是正确的。 You never do anything with that line, so it's lost. 您永远不会对那条线做任何事情,所以就迷路了。

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

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