I think problem comes from the list, when i try to initialize it with an initial size, the debugger shows me that it's size is still 0. When i try to add to the list using list.add(index, file), it throws me an IndexOutOfBounds exception.
package com.codegym.task.task18.task1825;
import java.io.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
/*
Building a file
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
if (fileName.equals("end")) return;
final String FINAL_FILE_NAME = cutString(fileName);
ArrayList<InputStream> streamsList = new ArrayList<>();
while (!fileName.equals("end")) {
BufferedInputStream file = new BufferedInputStream(new FileInputStream(fileName));
int index = getIndexFromName(fileName);
streamsList.add(file);
fileName = reader.readLine();
}
int count = 0;
for (InputStream s : streamsList) {
count += s.available();
}
byte[] buffer = new byte[count];
for (InputStream i : streamsList) {
int j = 0;
while (i.available() > 0) {
i.read(buffer);
j++;
}
i.close();
}
BufferedWriter finalFile = new BufferedWriter(new FileWriter(FINAL_FILE_NAME, true));
for (int i : buffer) {
finalFile.append((char) i);
}
finalFile.close();
reader.close();
}
public static String cutString(String name) {
String cutString = "";
int indexOfDot = 0;
for (int i = name.length() - 1; i >= 0; i--) {
char c = name.charAt(i);
if (c == '.') {
indexOfDot = i;
break;
}
}
for (int i = 0; i < name.length(); i++) {
if (i >= indexOfDot) break;
cutString += name.charAt(i);
}
return cutString;
}
public static int getIndexFromName(String name) {
String sIndex = "";
ArrayList<Character> list = new ArrayList<>();
for (int i = name.length() - 1; i >= 0; i--) {
try {
char c = name.charAt(i);
int temp = Integer.parseInt(String.valueOf(c));
list.add(c);
}
catch (Exception e) {
break;
}
}
for (int i = list.size() - 1; i >= 0; i--) {
sIndex += list.get(i);
}
return Integer.parseInt(sIndex);
}
}