Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level18.lesson10.home10;
- /* Собираем файл
- Собираем файл из кусочков
- 1. Считывать с консоли имена файлов
- Каждый файл имеет имя: [someName].partN.
- Например, Lion.avi.part1, Lion.avi.part2, ..., Lion.avi.part37.
- 2. Имена файлов подаются в произвольном порядке. Ввод заканчивается словом "end"
- В папке, где находятся все прочтенные файлы, создать файл без приставки [.partN]. Например, Lion.avi
- В него переписать все байты из файлов-частей используя буфер.
- Файлы переписывать в строгой последовательности, сначала первую часть,
- потом вторую, ..., в конце - последнюю.
- Закрыть потоки. Не использовать try-with-resources
- */
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class Solution {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String fileName;
- boolean stopInput = false;
- HashMap<String, String> fileList = new HashMap<String, String>();
- File specFile = null;
- while (!stopInput) {
- fileName =reader.readLine();
- if (fileName.equals("end")) {
- stopInput = true;
- }
- else {
- specFile = new File(fileName);
- if(specFile.exists() && !specFile.isDirectory()) {
- fileList.put(fileName, returnExt(fileName));
- }
- }
- }
- fileList= sortByComparator(fileList);
- /*---------------Print file list_to delete---*/
- for (Map.Entry<String, String> entry : fileList.entrySet()) {
- String key = entry.getKey();
- String value = entry.getValue();
- System.out.println("key, " + key + " value " + value);
- }
- /*---------------------------------------------*/
- reader.close();
- }
- public static String returnExt (String someName) {
- int i = someName.lastIndexOf('.');
- return someName.substring(i+1);
- }
- private static HashMap<String, String> sortByComparator(Map<String, String> unsortMap) {
- // Convert Map to List
- List<Map.Entry<String, String>> list =
- new LinkedList<Map.Entry<String, String>>(unsortMap.entrySet());
- // Sort list with comparator, to compare the Map values
- Collections.sort
- (list, new Comparator<Map.Entry<String, String>>() {
- public int compare(Map.Entry<String, String> o1,
- Map.Entry<String, String> o2) {
- return (o1.getValue()).compareTo(o2.getValue());
- }
- });
- // Convert sorted map back to a Map
- HashMap<String, String> sortedMap = new LinkedHashMap<String, String>();
- for (Iterator<Map.Entry<String, String>> it = list.iterator(); it.hasNext();) {
- Map.Entry<String, String> entry = it.next();
- sortedMap.put(entry.getKey(), entry.getValue());
- }
- return sortedMap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement