По существу то все условия выполнены (или нет?). Поток был единственный, для записи, и был закрыт. Буфер используется.
package com.javarush.task.task18.task1825;
import com.sun.org.apache.xpath.internal.objects.XString;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
Собираем файл
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
List<String> files = new ArrayList<>();
while (true) {
String s = reader.readLine();
if (s.equals("end")) {
break;
}
files.add(s);
}
FileCollector fileCollector = new FileCollector(files);
ByteArrayOutputStream bs = fileCollector.getByteArrayOutputStream();
String resultFile = fileCollector.getParent() + File.separator + fileCollector.getResultFileName();
FileOutputStream fileOutputStream = new FileOutputStream(resultFile);
fileOutputStream.write(bs.toByteArray());
fileOutputStream.close();
}
private static class FileCollector {
private String resultFileName = "";
private Path parent;
private ByteArrayOutputStream byteArrayOutputStream;
public FileCollector(List<String> list) throws IOException {
// Here we are checking the list has no elements whom we don't need
// if we will find the elements like that, we just will remove it
for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
Matcher m = Pattern.compile(".+\\..+\\.(part)\\d+").matcher(iterator.next());
if (!m.find()) {
iterator.remove();
}
}
// Furthermore if we had gotten the empty list after the last checking, we will throw IAE
String[] files = list.toArray(new String[0]);
if (files[0] == null) {
throw new IllegalArgumentException();
}
// Get the directory ...
Path p = Paths.get(files[0]);
this.parent = p.getParent();
// ... and the final file name that we needed
String subStings[] = p.getFileName().toString().split("\\.");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < subStings.length - 1; i++) {
sb.append(subStings[i]).append(".");
}
sb.deleteCharAt(sb.length() - 1);
this.resultFileName = sb.toString();
// get the sorted array with correctly order that we needed to read a bytes
Arrays.sort(files);
// and are reading the data into buffer from the files
this.byteArrayOutputStream = new ByteArrayOutputStream();
for (String paths : files) {
Path readingFile = Paths.get(paths);
if (Files.notExists(readingFile)) {
throw new FileNotFoundException();
}
byteArrayOutputStream.write(Files.readAllBytes(readingFile));
}
}
public String getResultFileName() {
return resultFileName;
}
public String getParent() {
return parent.toString();
}
public ByteArrayOutputStream getByteArrayOutputStream() {
return byteArrayOutputStream;
}
}
}
Действительно, нужно использовать буфер BufferedOutPutStream и BufferedInputStream. Кому-то наверно премию выдали за экономию символов при наборе условий задач. Пока не нагородил вот это, ни хотело принимать: UPDATE: Хотя, если нажать на кнопочку "Получить правильное решение", приходит код, в котором заместо оберток используется вообще обычный байтовый массив: Вот и как это понимать?