Actually, code is working pretty fine
package com.codegym.task.task18.task1825;
import java.io.*;
import java.util.*;
/*
Building a file
*/
public class Solution {
public static void main(String[] args) throws IOException {
ArrayList<InputStream> streamList = new ArrayList<>();
ArrayList<Integer> buffer = new ArrayList<>();
Scanner sc = new Scanner(System.in);
String fileName = null;
int partIndex = 0;
String newFileName = null;
while (true) {
try {
fileName = sc.nextLine();
if (fileName.equals("end")) break;
partIndex = fileName.indexOf("part");
String getNumber = fileName.substring(partIndex+4,fileName.length()); // cut off number from fileName
int number = Integer.parseInt(getNumber);
newFileName = fileName.substring(0, partIndex-1); // cut out new file name
//System.out.println(number);
streamList.add(number-1, new FileInputStream(fileName)); //part1 will go to 0 index
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
for (InputStream is : streamList) {
while (is.available() > 0) {
buffer.add(is.read());
}
}
//System.out.println(newFileName);
OutputStream os = new FileOutputStream(newFileName);
for (int b : buffer) {
os.write(b);
}
for (InputStream is : streamList) {
is.close();
}
os.close();
}
}