My code is supposed to read several file names from the console until the "end", sort them by the number that follows the "part" word in the name (tried to do so with the help of my Sorter class), create a file in the same directory and name it the same way as the other files, just without "partN", and then copy the data from the read files to the one I created. According to my logic, I did all of that, while the validator doesn't agree with me at all:(. I wonder what I'm missing here.
package com.codegym.task.task18.task1825;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/*
Building a file
*/
public class Solution {
public static void main(String[] args) throws IOException, IndexOutOfBoundsException {
//• The program must read file names from the console until the word "end" is entered. - OK
BufferedReader breader = new BufferedReader(new InputStreamReader(System.in));
TreeSet<String> fileNames = new TreeSet<String>(new Sorter());
String fileName = null;
while (breader.ready()) {
fileName = breader.readLine();
fileNames.add(fileName);
if (fileName.equalsIgnoreCase("end")) break;
}
//find out the name of the folder where the files are stored (one file's location is enough, I guess?)
File file = new File(fileName);
File directory = file.getParentFile();
String directoryName = directory.getAbsolutePath();
//create a file in that directory under the name of any of the other files, just without "partN"
String[] fileNameSplit = fileName.split("part");
String targetFileName = fileNameSplit[0];
File targetFile = new File(directoryName, targetFileName);
//• Create a stream to write to the file without the "part" suffix (".<partN>")
// in the folder with all the "part" files. - NOT ACCEPTED BY THE VALIDATOR
FileOutputStream outputStream = new FileOutputStream(targetFileName); //or is it better to use targetFile.getAbsolutePath()?
//• Copy all the bytes from the *.partN files to the new file. - NOT ACCEPTED BY THE VALIDATOR
//• You should use a buffer for the reading and writing. - I THOUGHT byte[] byteArray WAS A BUFFER...
for (String fn : fileNames) {
FileInputStream inputStream = new FileInputStream(fn);
byte[] byteArray = new byte[inputStream.available()];
inputStream.read(byteArray);
outputStream.write(byteArray);
inputStream.close(); //each input stream is closed inside the loop
}
//• The file streams must be closed. - NOT ACCEPTED, AS "NOT ALL FILE INPUT STREAMS WERE CLOSE" (but I closed them inside the loop above)
breader.close();
outputStream.close();
//• Don't use static variables. - OK
}
public static class Sorter implements Comparator<String> {
@Override
public int compare(String s1, String s2) {
int n1 = Integer.parseInt(s1.split("part")[1]);
int n2 = Integer.parseInt(s2.split("part")[1]);
return n2 - n1;
}
}
}
fileName.substring(0, fileName.lastIndxOf('.'));
then the result is c:\Temp\lion.avi and you don't have to do any more path construction work. I recommend getting used to try-with-resources. That makes in-out streams a lot easier (and later nio of course).