So I made this algorithm where I add all Strings (filenames) from the console to an ArrayList, then I add the file part numbers to a TreeSet (lines 25 - 28) then to an ArrayList (line 29), then I iterate (lines 30 - 46) through the filename ArrayList to find the one which part number comes first by comparing it to the part-number Arraylist, then create the streams to copy the content, the output is correct on my pc with my files, but still doesn't validate :. - (
package com.codegym.task.task18.task1825;
import java.io.*;
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 s;
ArrayList<String> strings = new ArrayList<>();
while (!(s = reader.readLine()).equals("end")){
strings.add(s);
}
File file = new File(strings.get(0).substring(0,strings.get(0).indexOf("part")));
file.createNewFile();
TreeSet < Integer > integers = new TreeSet<>();
for (String s1: strings) {
integers.add(Integer.parseInt(s1.substring(s1.indexOf("part") + 4,s1.length())));
}
ArrayList<Integer> arrayList = new ArrayList<>(integers);
for (int a = 0; a < integers.size(); a++) {
for (String s1 : strings
) {
if (Integer.parseInt(s1.substring(s1.indexOf("part") + 4,s1.indexOf('.',s1.indexOf("part") + 4))) == arrayList.get(a)) {
FileInputStream inputStream = new FileInputStream(s1);
FileOutputStream outputStream = new FileOutputStream(file, true);
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
for (byte b : buffer
) {
outputStream.write(b);
}
inputStream.close();
outputStream.close();
}
}
}
/*for (Integer i : integers
) {
System.out.println(i);
}*/
}
}