Не прошло по всем, кроме последнего требования(
package com.javarush.task.task18.task1825;
import java.io.*;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
/*
Собираем файл
*/
public class Solution {
public static void main(String[] args) throws IOException {
TreeSet<String> fileParts = new TreeSet<String>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String prefix1 = o1.substring(o1.lastIndexOf(".part") + 5, o1.lastIndexOf("."));
String prefix2 = o2.substring(o2.lastIndexOf(".part") + 5, o1.lastIndexOf("."));
return Integer.parseInt(prefix1) - Integer.parseInt(prefix2);
}
});
readFilesName(fileParts);
String filePart = fileParts.first();
String partN = filePart.substring(filePart.lastIndexOf(".part"), filePart.lastIndexOf("."));
String filePath = filePart.replaceAll(partN, "");
File file = new File(filePath);
writeAllDataIn(file.getPath(), fileParts);
}
static void writeAllDataIn(String des, Set<String> set) throws FileNotFoundException, IOException {
for (String source : set) {
byte[] buffer = null;
try(BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(des, true))){
buffer = new byte[in.available()];
in.read(buffer);
out.write(buffer);
}
}
}
public static void readFilesName(Set<String> set) throws IOException {
try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))){
String s = null;
while(!(s = reader.readLine()).equals("end")) {
set.add(s);
}
}
}
}