package com.javarush.task.task18.task1825;

import java.io.*;
import java.util.Map;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;

/* 
Собираем файл
*/

public class Solution {

    ///Users/pshynin/Desktop/data1.txt.part1
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        SortedMap<Integer, String> sorted = new TreeMap<>();
        String entry;
        String file = null;
        while (!(entry = reader.readLine()).contains("end")) {
            String key = entry.substring(entry.lastIndexOf(".part") + 5).trim();
            file = entry.replace(".part" + key, "");
            sorted.put(Integer.parseInt(key), entry);
        }
        reader.close();

        FileOutputStream output = new FileOutputStream(Objects.requireNonNull(file), true);
        for (Map.Entry<Integer, String> set : sorted.entrySet()) {
            FileInputStream input = new FileInputStream(set.getValue());
            while (input.available() > 0) {
                byte[] buffer = new byte[input.available()];
                input.read(buffer);
                output.write(buffer);
            }
            input.close();
        }
        output.close();
    }
}
