Dears, Will it be possible to check why requirement 3 is getting fail to validate?
Thanks for your help in advance.
package com.codegym.task.task18.task1825;
import java.io.*;
import java.util.*;
/*
Building a file
Let's build a file from various pieces.
Read file names from the console.
Each file has a name: <someName>.partN.
For example, Lion.avi.part1, Lion.avi.part2, ..., Lion.avi.part37.
The file names are supplied in random order. The word "end" is used to stop reading in file names.
In the folder where all the files are located, create a file without the "part" suffix, i.e. without ".<partN>".
For example, Lion.avi.
Use a buffer to copy all the bytes from the partial files to the created file.
Copy the first in the proper order, first the first part, then the second, ..., finally - the last part.
Close the streams.
Requirements:
1. The program must read file names from the console until the word "end" is entered.
2. Create a stream to write to the file without the "part" suffix (".<partN>") in the folder with all the "part" files.
3. Copy all the bytes from the *.partN files to the new file.
4. You should use a buffer for the reading and writing.
5. The file streams must be closed.
6. Don't use static variables.
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//Reading the file name from the user.
String fileName;
boolean isContinue=true;
FileInputStream fileInputStream;
BufferedInputStream bufferedInputStream;
SortedMap<Integer,String> map = new TreeMap<>();
FileOutputStream fileOutputStream;
BufferedOutputStream bufferedOutputStream;
List<String> fileContent = new ArrayList<>();
//Reading the fileName
while (isContinue) {
fileName = reader.readLine();
if (fileName.equals("end")) {
//Closing the stream as user has entered the work "end"
reader.close();
isContinue = false;
}
else {
//putting them in the ascending order in the map.
map.put(Integer.valueOf(fileName.substring(fileName.lastIndexOf(".") + 5)), fileName);
}
}
for(Map.Entry<Integer,String> map1 : map.entrySet()){
fileName = map1.getValue();
String modifiedFileName = fileName.substring(0,fileName.lastIndexOf("."));
//create a file without the "part" suffix
fileInputStream = new FileInputStream(fileName);
fileOutputStream = new FileOutputStream(modifiedFileName);
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
//Reading and writing the file data.
while(bufferedInputStream.available()>0 ){
fileContent.add(String.valueOf((char)bufferedInputStream.read()));
}
for(String s: fileContent){
byte[] b = s.getBytes();
fileOutputStream.write(b,0,b.length);
}
//closing the stream
bufferedInputStream.close();
bufferedOutputStream.close();
fileInputStream.close();
fileOutputStream.close();
}
}
}