Hi,
Spent several days, have tried different options to implement the task. All the time fail to pass requirements #2-#5
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 {
String prefix="C:\\Users\\k.shayakhmetov\\Desktop\\CodeGym\\CodeGymTasks\\CodeGymTasks\\2.JavaCore\\src\\com\\codegym\\task\\task18\\task1825\\"; //read file names from the console
//read in file names
Scanner scanner=new Scanner(System.in);
ArrayList<String> fileNames=new ArrayList<>();
String temp;
while (!(temp=scanner.nextLine()).equals("")){
if (temp.equals("end")) {
scanner.close();
break;
}
else {
fileNames.add(temp);
System.out.println(temp);
}
}
//extract the fileName without part#
String tempFileName=fileNames.get(0);
int index=tempFileName.indexOf("part");
String fileToWrite=tempFileName.substring(0,index-1);
TreeMap<Integer, String> map=new TreeMap<>();
for (String fileName : fileNames) {
String[] split = fileName.split("\\.");
int key = Integer.parseInt(String.valueOf(split[2].charAt(split[2].length() - 1)));
map.put(key, fileName);
}
FileOutputStream fileOutputStream=new FileOutputStream(new File(prefix+fileToWrite), true);
BufferedOutputStream bos=new BufferedOutputStream(fileOutputStream, 1000);
readFile(map, bos);
bos.close();fileOutputStream.close();
}
public static void readFile(TreeMap<Integer, String> map, BufferedOutputStream bos) throws IOException {
String prefix="C:\\Users\\k.shayakhmetov\\Desktop\\CodeGym\\CodeGymTasks\\CodeGymTasks\\2.JavaCore\\src\\com\\codegym\\task\\task18\\task1825\\";
for (Map.Entry<Integer, String> pair:map.entrySet()){
FileInputStream fileInputStream=new FileInputStream(prefix+pair.getValue());
BufferedInputStream reader=new BufferedInputStream(fileInputStream, 1000);
byte[] buffer = new byte[reader.available()];
byte read;
while ((read = (byte)reader.read(buffer, 0, buffer.length)) > 0)
{
bos.write(buffer, 0, read);
}
reader.close(); fileInputStream.close();
}
}
}