What is the secret to passing this task
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, FileNotFoundException {
String filenames;
ArrayList<String> fileparts = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(!(filenames = reader.readLine()).equals("end"))
{
fileparts.add(filenames);
}
//Find the last skip Charectar( this will get the file path + and the start of the file name)
int skipIndex=Solution.lastIndexof(fileparts.get(0),'\\');
//Find the last period(his will get the full file name excluding file type
int periodindex=Solution.lastIndexof(fileparts.get(0),'.');
//get filetype by getting all substring after the last period
String fileType = fileparts.get(0).substring(periodindex);
//get the folder and file parts' path
String filePath = fileparts.get(0).substring(0,skipIndex+1);
//get full file name excluding file type
String fullfilename=fileparts.get(0).substring(skipIndex+1,periodindex);
//get the last period from full file name to exclude "partN"
periodindex=Solution.lastIndexof(fullfilename, '.');
//finally filename can be used to write all the file parts
String filename= fullfilename.substring(0,periodindex);
//open file output stream
String writefile=filePath+filename+fileType;
FileOutputStream out =new FileOutputStream(writefile);
//created a list to have all teh file sorted by thier part number
//sorting logic is to use the part number as the index of the array
String[] sortedlist = new String[fileparts.size()];
for (String s: fileparts) {
int start= s.indexOf("part");
int end = Solution.lastIndexof(s,'.');
start= start+4;
int number =Integer.parseInt(s.substring(start,end));
sortedlist[number-1]=s;
}
//finally start reading fron teh sorted list and write to the ouput file
for( String s:sortedlist){
FileInputStream in = new FileInputStream(s);
byte[] readbuffer = new byte[in.available()];
in.read(readbuffer);
out.write(readbuffer);
in.close();
}
out.close();
}
public static int lastIndexof(String s, char c) {
int index = 0, finalIndex = 0;
while (true) {
index = s.indexOf(c, index);
if (index > finalIndex) {
finalIndex = index;
index++;
} else {
index = 0;
break;
}
}
return finalIndex;
}
}