Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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*/ {
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String fileName;
- boolean stopInput = false;
- TreeMap<Integer, String> fileList = new TreeMap<Integer, String>();
- while (!stopInput) {
- fileName =reader.readLine();
- if (fileName.equals("end")) {
- stopInput = true;
- }
- else {
- fileList.put(returnIndex(fileName),fileName);
- }
- }
- String filePath = new File ((fileList.firstEntry()).getValue()).getParent()+"\\";
- String fileFirstName = fileFirstName((fileList.firstEntry()).getValue());
- String fileExtn = fileType((fileList.firstEntry()).getValue());
- String createdFileName = filePath+fileFirstName+"."+fileExtn;
- /*---------------Create new file---------------*/
- File newFile = new File(createdFileName);
- if (!newFile.exists()) {
- newFile.createNewFile();
- }
- else {
- if (newFile.length()!=0) {
- newFile.delete();
- newFile.createNewFile();
- }
- }
- for (Map.Entry<Integer,String> entry:fileList.entrySet()) {
- copy(entry.getValue(),createdFileName);
- }
- }
- catch (Exception e) {
- }
- /*-------------------End of Create new file-----------*/
- }/*---------------End of main function-------------*/
- public static int returnIndex (String someName) {
- int i = someName.lastIndexOf(".part");
- return Integer.parseInt(someName.substring(i+5));
- }
- public static String fileType (String inputName) {
- int beginIndex = inputName.indexOf(".")+1;
- int lastIndOf = inputName.lastIndexOf(".");
- String extention = inputName.substring(beginIndex,lastIndOf);
- return extention;
- }
- public static String fileFirstName (String inputName) {
- int beginIndex = inputName.indexOf(".")+1;
- String someName = inputName.substring(inputName.lastIndexOf("\\")+1,beginIndex-1);
- return someName;
- }
- /*------------------Copy File------------------*/
- public static void copy (String src, String dest) throws IOException{
- FileInputStream in = new FileInputStream(src);
- FileOutputStream out = new FileOutputStream(dest, true);
- BufferedInputStream bufferedInputStream = new BufferedInputStream(in,1024);
- BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out,1024);
- int c;
- try
- {
- while ((c=bufferedInputStream.read())!=-1) {
- bufferedOutputStream.write(c);
- }
- } finally
- { bufferedInputStream.close();
- bufferedOutputStream.flush();
- bufferedOutputStream.close();
- in.close();
- out.close();
- }
- }
- /*---------------------------------------------*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement