![](https://cdn.javarush.com/images/system/dbd8bd2f-2325-4075-b942-6f6c9b2d6d7d/1024.jpeg)
![](https://cdn.javarush.com/images/system/a4c58a44-5c08-44dd-8bfb-e3f546b1af1f/1024.jpeg)
package com.javarush.task.task18.task1825;
import java.io.*;
import java.util.*;
/*
Собираем файл
*/
public class Solution {
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
String string;
while (!(string = scanner.nextLine()).equals("end")) {
list.add(string);
}
Map<Integer, String > map = new TreeMap<>();
int lastIndex = list.get(0).indexOf("part");
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
String partStr = s.substring(lastIndex + 4);
int part = Integer.parseInt(partStr);
map.put(part, s);
}
String fullName = list.get(0).substring(0, lastIndex-1);
// System.out.println(fullName);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(fullName), true));
Set set = map.entrySet();
for (Object o : set) {
Map.Entry m = (Map.Entry) o;
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(new File((String) m.getValue())));
byte[] bytes = new byte[bufferedInputStream.available()];
bufferedInputStream.read(bytes);
bufferedOutputStream.write(bytes);
bufferedInputStream.close();
}
bufferedOutputStream.close();
}
}