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 IOException {
String prefix="C:\\Users\\k.shayakhmetov\\Desktop\\CodeGym\\CodeGymTasks\\CodeGymTasks\\2.JavaCore\\src\\com\\codegym\\task\\task18\\task1825\\"; //read file names from the console
//read in file names
Scanner scanner=new Scanner(System.in);
ArrayList<String> fileNames=new ArrayList<>();
String temp;
while (!(temp=scanner.nextLine()).equals("")){
if (temp.equals("end")) {
scanner.close();
break;
}
else {
fileNames.add(temp);
System.out.println(temp);
}
}
//extract the fileName without part#
String tempFileName=fileNames.get(0);
int index=tempFileName.indexOf("part");
String fileToWrite=tempFileName.substring(0,index-1);
TreeMap<Integer, String> map=new TreeMap<>();
for (String fileName : fileNames) {
String[] split = fileName.split("\\.");
int key = Integer.parseInt(String.valueOf(split[2].charAt(split[2].length() - 1)));
map.put(key, fileName);
}
FileOutputStream fileOutputStream=new FileOutputStream(new File(prefix+fileToWrite), true);
BufferedOutputStream bos=new BufferedOutputStream(fileOutputStream, 1000);
readFile(map, bos);
bos.close();fileOutputStream.close();
}
public static void readFile(TreeMap<Integer, String> map, BufferedOutputStream bos) throws IOException {
String prefix="C:\\Users\\k.shayakhmetov\\Desktop\\CodeGym\\CodeGymTasks\\CodeGymTasks\\2.JavaCore\\src\\com\\codegym\\task\\task18\\task1825\\";
for (Map.Entry<Integer, String> pair:map.entrySet()){
FileInputStream fileInputStream=new FileInputStream(prefix+pair.getValue());
BufferedInputStream reader=new BufferedInputStream(fileInputStream, 1000);
byte[] buffer = new byte[reader.available()];
byte read;
while ((read = (byte)reader.read(buffer, 0, buffer.length)) > 0)
{
bos.write(buffer, 0, read);
}
reader.close(); fileInputStream.close();
}
}
}
Kaiyr Shayakhmetov
Level 22
The code seems working but fails to pass verification
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
17 August 2020, 18:02
#1 get rid of prefix in both locations. This code does not work on my computer and it should. It will not work for the validation.
#2 along with the first point, splitting by decimal at line 38 wont result in exactly 3 values in the array on different computers. On my computer it throws an exception at that line and most likely will on any computer
#3 line 39 only gets the last digit of "partN", however there could hundreds or thousands of parts. The code should be able to get the correct part number to add to the tree
0
Kaiyr Shayakhmetov
18 August 2020, 13:16
Hi,
Thank you so much. Tried your recommendations and finally managed to pass validation.
+1