Created
July 14, 2016 16:29
-
-
Save josinSbazin/ef051b0fc95e3e483d362be09f35cf65 to your computer and use it in GitHub Desktop.
level18.lesson10.home10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level18.lesson10.home10; | |
/* Собираем файл | |
Собираем файл из кусочков | |
Считывать с консоли имена файлов | |
Каждый файл имеет имя: [someName].partN. Например, Lion.avi.part1, Lion.avi.part2, ..., Lion.avi.part37. | |
Имена файлов подаются в произвольном порядке. Ввод заканчивается словом "end" | |
В папке, где находятся все прочтенные файлы, создать файл без приставки [.partN]. Например, Lion.avi | |
В него переписать все байты из файлов-частей используя буфер. | |
Файлы переписывать в строгой последовательности, сначала первую часть, потом вторую, ..., в конце - последнюю. | |
Закрыть потоки. Не использовать try-with-resources | |
*/ | |
import java.io.*; | |
import java.util.Map; | |
import java.util.TreeMap; | |
public class Solution { | |
public static void main(String[] args) throws IOException { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
Map<Integer, Byte[]> buffers = new TreeMap<Integer, Byte[]>(); | |
String t; | |
String[] out = new String[2]; | |
FileInputStream fis = null; | |
while (!(t = reader.readLine()).equals("end")) { | |
fis = new FileInputStream(t); | |
byte[] b = new byte[fis.available()]; | |
fis.read(b); | |
Byte[] byteToMap = toByte(b); | |
out = t.split(".part"); | |
Integer intToMap = Integer.parseInt(out[1]); | |
buffers.put(intToMap, byteToMap); | |
} | |
reader.close(); | |
fis.close(); | |
String outFileName = out[0]; | |
out=null; | |
FileOutputStream fos = new FileOutputStream(outFileName); | |
for (Map.Entry<Integer, Byte[]> map : buffers.entrySet()) { | |
fos.write(toPrimitiveByte(map.getValue())); | |
} | |
fos.close(); | |
} | |
public static Byte[] toByte (byte[] b) { | |
int size = b.length; | |
Byte[] result = new Byte[size]; | |
for (int i = 0; i < size; i++) { | |
result[i] = b[i]; | |
} | |
return result; | |
} | |
public static byte[] toPrimitiveByte (Byte[] b) { | |
int size = b.length; | |
byte[] result = new byte[size]; | |
for (int i = 0; i < size; i++) { | |
result[i] = b[i]; | |
} | |
return result; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment