Not sure why, I can't spot any infinite loops
Edit: I removed line 53 and changed line 57 to:
FileInputStream fileInputStream = new FileInputStream(s);
now it's failing all the conditions except the first and last with the mentor recommendation:
The file output stream was not closed.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 Exception{
//read files and add to list
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> allFiles = new ArrayList<>();
String fileName;
while (!(fileName = reader.readLine()).equals("end")){
allFiles.add(fileName);
}
reader.close();
//sort files into correct order
ArrayList<Integer> fileNumbers = new ArrayList<>();
for (String allFile : allFiles) {
String[] fileParts = allFile.split(".");//split file names based on "."
String fileNumber = fileParts[fileParts.length - 1].replaceAll("\\D+", ""); //find the last part i.e. the file number and remove letters
fileNumbers.add(Integer.parseInt(fileNumber));
}
Collections.sort(fileNumbers);
ArrayList<String> sortedFileNames = new ArrayList<>();
for (Integer i : fileNumbers){
for (String s : allFiles){
String[] fileParts = s.split(".");
String fileNumber = fileParts[fileParts.length - 1].replaceAll("\\D+", "");
if (i.toString().equals(fileNumber)){
sortedFileNames.add(s);
}
}
}
//create new file
String[] getFileName = sortedFileNames.get(0).split(".");
String newfileName = "";
for (int i =0; i<getFileName.length-2; i++){
newfileName += (getFileName[i] + ".");
}
newfileName += getFileName[getFileName.length-2];
File newFile = new File(newfileName);
//write contents of file to new file
FileOutputStream fileOutputStream = new FileOutputStream(newFile, true);
for (String s : sortedFileNames){
FileInputStream fileInputStream = new FileInputStream(s);
byte[] buffer = new byte[fileInputStream.available()];
while (fileInputStream.available()>0){
fileInputStream.read(buffer);
fileOutputStream.write(buffer);
}
fileInputStream.close();
}
fileOutputStream.close();
}
}