Advertisement
jaVer404

level18.lesson10.home10(night_built)

Jan 17th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.62 KB | None | 0 0
  1. package com.javarush.test.level18.lesson10.home10;
  2.  
  3. /* Собираем файл
  4. Собираем файл из кусочков
  5. Считывать с консоли имена файлов
  6. Каждый файл имеет имя: [someName].partN. Например, Lion.avi.part1, Lion.avi.part2, ..., Lion.avi.part37.
  7. Имена файлов подаются в произвольном порядке. Ввод заканчивается словом "end"
  8. В папке, где находятся все прочтенные файлы, создать файл без приставки [.partN]. Например, Lion.avi
  9. В него переписать все байты из файлов-частей используя буфер.
  10. Файлы переписывать в строгой последовательности, сначала первую часть, потом вторую, ..., в конце - последнюю.
  11. Закрыть потоки. Не использовать try-with-resources
  12. */
  13.  
  14. /*
  15. * Прочитать файлы (имена)
  16. * Загнать их в карту с сортировкой
  17. * Создать адрес (папки файла)
  18. * Взять расширение
  19. * В той же папке (можно взять по имени файла) создать файл с именем и заданым разрешением
  20. * И поочередно дописать туда файлы из карты
  21. *
  22. * */
  23.  
  24. import java.io.*;
  25. import java.util.Map;
  26. import java.util.TreeMap;
  27.  
  28. public class Solution {
  29.     public static void main(String[] args) /*throws IOException*/ {
  30.         try {
  31.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  32.         String fileName;
  33.         boolean stopInput = false;
  34.         TreeMap<Integer, String> fileList = new TreeMap<Integer, String>();
  35.         while (!stopInput) {
  36.             fileName =reader.readLine();
  37.             if (fileName.equals("end")) {
  38.                 stopInput = true;
  39.             }
  40.             else {
  41.                 fileList.put(returnIndex(fileName),fileName);
  42.             }
  43.         }
  44.             String filePath = new File ((fileList.firstEntry()).getValue()).getParent()+"\\";
  45.             String fileFirstName = fileFirstName((fileList.firstEntry()).getValue());
  46.             String fileExtn = fileType((fileList.firstEntry()).getValue());
  47.             String createdFileName = filePath+fileFirstName+"."+fileExtn;
  48.  
  49.  
  50. /*---------------Create new file---------------*/
  51.  
  52.             File newFile = new File(createdFileName);
  53.             if (!newFile.exists()) {
  54.                 newFile.createNewFile();
  55.             }
  56.             else {
  57.                 if (newFile.length()!=0) {
  58.                 newFile.delete();
  59.                 newFile.createNewFile();
  60.                 }
  61.             }
  62.             for (Map.Entry<Integer,String> entry:fileList.entrySet()) {
  63.                 copy(entry.getValue(),createdFileName);
  64.             }
  65.         }
  66.         catch (Exception e) {
  67.         }
  68.  
  69. /*-------------------End of Create new file-----------*/
  70.     }/*---------------End of main function-------------*/
  71.     public static  int returnIndex (String someName) {
  72.         int i = someName.lastIndexOf(".part");
  73.         return Integer.parseInt(someName.substring(i+5));
  74.     }
  75.     public static String fileType (String inputName) {
  76.         int beginIndex = inputName.indexOf(".")+1;
  77.         int lastIndOf = inputName.lastIndexOf(".");
  78.         String extention = inputName.substring(beginIndex,lastIndOf);
  79.         return extention;
  80.     }
  81.     public static String fileFirstName (String inputName) {
  82.         int beginIndex = inputName.indexOf(".")+1;
  83.         String someName = inputName.substring(inputName.lastIndexOf("\\")+1,beginIndex-1);
  84.         return someName;
  85.     }
  86.     /*------------------Copy File------------------*/
  87.     public static void copy (String src, String dest) throws IOException{
  88.  
  89.         FileInputStream in = new FileInputStream(src);
  90.         FileOutputStream out = new FileOutputStream(dest, true);
  91.         BufferedInputStream bufferedInputStream = new BufferedInputStream(in,1024);
  92.         BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out,1024);
  93.         int c;
  94.         try
  95.         {
  96.  
  97.             while ((c=bufferedInputStream.read())!=-1) {
  98.                 bufferedOutputStream.write(c);
  99.             }
  100.         } finally
  101.         {   bufferedInputStream.close();
  102.             bufferedOutputStream.flush();
  103.             bufferedOutputStream.close();
  104.             in.close();
  105.             out.close();
  106.         }
  107.     }
  108. /*---------------------------------------------*/
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement