Merge two text files and sort them to a third file

advertisements

I'm trying to make a program that will merge two different text files into a third file which is sorted in ascending order. Here is what the two files have within them

File 1: 12 23 34 45 56 67 69 123 133

File 2: 4 5 10 20 35 44 100 130 150 160 180

And here is the code I have so far:

    try
    {

        FileReader file1=new FileReader("D://School//text1.txt");
        Scanner scan = new Scanner(new File("D://School//text1.txt"));
        ArrayList<Integer> values = new ArrayList<Integer>();
        Collections.sort(values);      //sorting the values
        while(scan.hasNextInt()) values.add(scan.nextInt());

        FileReader file2=new FileReader("D://School//text2.txt");
        scan = new Scanner(new File("D://School//text2.txt"));
        values = new ArrayList<>();
        Collections.sort(values);       //sorting the values.
        while(scan.hasNextInt()) values.add(scan.nextInt());

        BufferedReader br1 = new BufferedReader (file1);
        BufferedReader br2 = new BufferedReader(file2);

        String temp1 = "";
        String temp2 = "";

        while(br1.readLine() !=null)
        {
            temp1=br1.readLine()+temp1;
        }
        while(br2.readLine()!=null)
        {
            temp2=br2.readLine()+temp2;
        }
        String temp = temp1 + temp2; 

        FileWriter fw=new FileWriter("D://School//text3.txt");
        char buffer[]=new char[temp.length()];
        temp.getChars(0,temp.length(),buffer,0);
        fw.write(buffer);
        file1.close();
        file2.close();
        fw.close();
    } 

    catch (IOException e)
    {
        e.printStackTrace();
    }

The program so far will be able to read and write the proper contents into the third file, however, it isn't sorting it into ascending order.

Here is what the program will print to the third file

12 23 34 45 56 67 69 123 133 4 5 10 20 35 44 100 130 150 160 180

I want it to be sorting it into the third file as so

4 5 10 12 20 23 34 35 44 45 56 67 69 100 123 130 133 150 160 180

Thanks in advance :)


I suggest you to use TreeSet, and i saw some problem in your code.

TreeSet<Integer> treeSet = new TreeSet<Integer>();
while(scan.hasNextInt()) treeSet.add(scan.nextInt());
scan = new Scanner(new File("D://School//text2.txt"));
//Do not new Collection!!!!!
while(scan.hasNextInt()) treeSet.add(scan.nextInt());

and you can for-each the collection

for (Integer i : treeSet) {
        System.out.println(i);
}

and you can try this, if you have jdk8.

    ArrayList<Integer> ints = new ArrayList<>();
    Files.readAllLines(Paths.get("D://School//text1.txt"), StandardCharsets.UTF_8).forEach(x -> Arrays.asList(x.split(" ")).stream().forEach(y -> ints.add(Integer.parseInt(y))));
    Files.readAllLines(Paths.get("D://School//text2.txt"), StandardCharsets.UTF_8).forEach(x -> Arrays.asList(x.split(" ")).stream().forEach(y -> ints.add(Integer.parseInt(y))));
    Collections.sort(ints);
    StringBuilder sb = new StringBuilder();
    ints.stream().map(x -> x + " ").forEach(sb::append);
    Files.write(Paths.get("D://School//text3.txt"),sb.toString().getBytes(),StandardOpenOption.WRITE,StandardOpenOption.CREATE);