My problem starts at line 52 - i have tried many ways to find an algorithm that resets the LOOP if the key is not correct.
package com.codegym.task.task18.task1825;

import javax.swing.plaf.synth.SynthTextAreaUI;
import java.io.*;
import java.util.*;

/*
Building a file

*/

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String fileName = "";
        String[] nameBroken = null;
        String filePath = "";

        BufferedInputStream fileInputStream = null;
        File createdFile = null;
        BufferedOutputStream fileOutputStream = null;

        SortedMap<Integer, String> fileNames = new TreeMap<>();

        //here I read all of the file names/paths and I add them in the Sorted Map that has
        //as key the number of the file and as value the complete path name
        while (true) {
            fileName = bufferedReader.readLine();
            if (fileName.equals("end")) {
                bufferedReader.close();
                break;
            } else filePath = fileName;

            //here i break down the file path using regex "part" so i can put the number after part as key
            //and the complete file name as value ex: E:\Movies\Filename.AVI.part3 - key is 3,
            //value is E:\Movies\Filename.AVI.part1
            nameBroken = fileName.split("part");
            fileNames.put(Integer.valueOf(nameBroken[1]), fileName);
        }
        bufferedReader.close();

        //all of the complete filenames are stored and the value is their path
        //now i need to write them as they are already in order

        nameBroken = filePath.split(".");
        //this creates THE BASE FILE that will contain all of the files. nameBroken[0] = E:\Movies\Filename
        //nameBroken[1] = avi
        createdFile = new File(nameBroken[0] + "." + nameBroken[1]);
        //this creates the stream with which I will write to the BASE FILE
        fileOutputStream = new BufferedOutputStream(new FileOutputStream(createdFile, true));

        int count = 1; //counter to ensure proper order of the files written
        //Map.Entry<Integer, String> entry : fileNames.entrySet()
        for (int i = 1; i < fileNames.size(); ) {
            //i need an algorithm that resets the entry set if the current entry is not the first value
            if (fileNames.containsKey(i)) {
                //if (fileNames.)
                fileInputStream = new BufferedInputStream(new FileInputStream(fileNames.get(i)));
                while (fileInputStream.available() > 0) fileOutputStream.write(fileInputStream.read());
                count += 1;
            }
        }

        fileInputStream.close();
        fileOutputStream.close();

    }
}