Created
July 28, 2016 08:41
-
-
Save cyberterror/2d96b2f5121cacb90a50d532bf43243f to your computer and use it in GitHub Desktop.
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.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.Set; | |
import java.util.TreeSet; | |
public class Solution { | |
public static void main(String[] args) throws IOException | |
{ | |
Set<String> files = new TreeSet<String>(); | |
Path path; | |
String targetFileName = ""; | |
String targetPath = ""; | |
String fileWithPath = ""; | |
byte[] bytes; | |
/** | |
* Считывать с консоли имена файлов | |
* Каждый файл имеет имя: [someName].partN. Например, Lion.avi.part1, Lion.avi.part2, ..., Lion.avi.part37.\ | |
* Имена файлов подаются в произвольном порядке. Ввод заканчивается словом "end" | |
*/ | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
while (true) { | |
String fileName = reader.readLine(); | |
if (fileName.equals("end")) break; | |
files.add(fileName); | |
} | |
reader.close(); | |
/** Получаем директорию и имя целевого файла как строку*/ | |
path = Paths.get(files.iterator().next().toString()); | |
for (String s : path.getFileName().toString().split("\\.")) { | |
if (s.contains("part")) break; | |
targetFileName += s + "."; | |
} | |
targetFileName = targetFileName.substring(0,targetFileName.length()-1); | |
targetPath = path.getParent().toString(); | |
/** Получаем полное имя файла с директорией в зависимости от ОС*/ | |
if (System.getProperty("os.name").equals("Linux")) { | |
fileWithPath = targetPath + "/" + targetFileName; | |
} | |
else if (System.getProperty("os.name").contains("Windows")) { | |
fileWithPath = targetPath + "\\" + targetFileName; | |
} | |
FileOutputStream fileOutputStream = new FileOutputStream(fileWithPath, true); | |
/** Читаем все файлы в буфер и дозаписываем в целевой файл*/ | |
for (String s : files) { | |
FileInputStream fileInputStream = new FileInputStream(s); | |
bytes = new byte[fileInputStream.available()]; | |
fileInputStream.read(bytes); | |
fileInputStream.close(); | |
fileOutputStream.write(bytes); | |
} | |
fileOutputStream.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment