S
S
Sergey Bogachev2019-09-14 19:48:33
Java
Sergey Bogachev, 2019-09-14 19:48:33

Java error in sorting two files into one, what is the error?

Hello, I am writing a JAVA program to sort two (sorted) files into one. Faced with the fact that the program displays the wrong result. I rewrote the program several times from the very beginning, but nothing helps. First tested on arrays, the program works fine. And when I connect streams for reading and writing to a file, the wrong sorting comes out. Help, I've already broken my head. Where is the mistake?

Here is the program code:
import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.FileWriter; 

public class MergeS {
  public static void main(String[] args) { 
      try { 
       FileReader fileReader1 = new FileReader("in11.txt");//Создает поток чтения и читает 
       FileReader fileReader2 = new FileReader("in22.txt");//Создает поток чтения и читает 
       BufferedReader bufferedReader1 = new BufferedReader(fileReader1); // Помещает прочитанное из fileReader1 в буфер bufferedReader1
       BufferedReader bufferedReader2 = new BufferedReader(fileReader2); // Помещает прочитанное из fileReader2 в буфер bufferedReader2
  
       String line1 = bufferedReader1.readLine(); // Читает строку из bufferedReader1 и присваивает ее line1
       String line2 = bufferedReader2.readLine(); // Читает строку из bufferedReader2 и присваивает ее line2
  
       FileWriter tmpFile = new FileWriter("out.txt", false); // Создает поток tmpFile для записи в файл
       int whichFileToRead = 0;

       boolean file_1_reader = true; 
       boolean file_2_reader = true; 
       
       while (file_1_reader || file_2_reader) { 
        if (file_1_reader == false) { 
           tmpFile.write(line2 + "\r\n"); 
           whichFileToRead = 2; 
           System.out.println("Write : " + line2);
        } else if (file_2_reader == false) { 
           tmpFile.write(line1 + "\r\n"); 
           whichFileToRead = 1; 
           System.out.println("Write : " + line1);
        } else { 
      	   String value1 = line1.substring(0); // получение символов из строки line1, и присваивает их value1
           String value2 = line2.substring(0); // получение символов из строки line2, и присваивает их value2 
           System.out.print("Sort : " + value1 + " vs "+ value2 + " ");

           int ans = value1.compareTo(value2); // сравнение двух значений value1 и value2 и присваивает это переменой ans
           if (ans < 0) { 
              tmpFile.write(line1 + "\r\n"); 
              whichFileToRead = 1; 
              System.out.println("Write : " + line1);
           } else if (ans > 0) { 
              tmpFile.write(line2 + "\r\n"); 
              whichFileToRead = 2; 
              System.out.println("Write : " + line2);
           } else if (ans == 0) { 
              tmpFile.write(line1 + "\r\n"); 
              whichFileToRead = 1; 
              System.out.println("Write : " + line1);
           } 
        } 
          if (whichFileToRead == 1) { 
             line1 = bufferedReader1.readLine(); // Читает строку из буфера bufferedReader1 и присваивает line1
             if (line1 == null) 
          	   file_1_reader = false; // Если line1 пустое, тогда file_1_reader присваивается значение false
          } else { 
             line2 = bufferedReader2.readLine(); // Читает строку из буфера bufferedReader2 и присваивает line2
             if (line2 == null) 
          	   file_2_reader = false; // Если line2 пустое, тогда file_2_reader присваивается значение false
          } 
       }
       tmpFile.close(); 
       bufferedReader1.close(); 
       bufferedReader2.close(); 
       fileReader1.close(); 
       fileReader2.close(); 
  
      } catch (Exception e) { 
      	System.out.println(e); 
      } 
  } 
}

This is what is written to the out.txt file:

10
11
21
23
24
40
41
50
65
75
76
77
78
78
78
78
86
900
2100
2200
2300
2400
2500
98
101
190
1100
1200
3000
5000

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2019-09-18
@cudu

Without reading all the code: You are using a FileWriter that writes data sequentially to a file, how is the previously written values ​​​​sorted?
You need to sort the read values ​​in memory, then write all already sorted ones to a file. Or use RandomAccessFile, which will complicate the already difficult code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question