public class Solution {
public static void main(String[] args) {
Scanner scanner=new Scanner(new BufferedInputStream(System.in));
Map<Integer, String> fileNameList=new TreeMap<>();
while (true){
String incommingMessage=scanner.nextLine();
if(incommingMessage.equals("end")) break;
fileNameList.put(Integer.parseInt(incommingMessage.substring(incommingMessage.indexOf(".partN")+5)), incommingMessage);
}
File into=new File(fileNameList.get(0).substring(0,fileNameList.get(0).indexOf(".part")));
try (FileOutputStream fos = new FileOutputStream(into);
BufferedOutputStream mergingStream = new BufferedOutputStream(fos)) {
for (String fileName : fileNameList.values()) {
Path p= Paths.get(fileName);
Files.copy(p, mergingStream);
Files.delete(p);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(fileNameList);
}
}
package com.javarush.task.task18.task1825;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
/*
Собираем файл
1. Программа должна считывать имена файлов с консоли, пока не будет введено слово "end".
2. Создай поток для записи в файл без суффикса [.partN] в папке, где находятся все считанные файлы.
3. В новый файл перепиши все байты из файлов-частей *.partN.
4. Чтение и запись должны происходить с использованием буфера.
5. Созданные для файлов потоки должны быть закрыты.
6. Не используй статические переменные.
*/
public class Solution {
public static void main(String[] args) {
Scanner scanner=new Scanner(new BufferedInputStream(System.in));
Map<Integer, String> fileNameList=new TreeMap<>();
while (true){
String incommingMessage=scanner.nextLine();
if(incommingMessage.equals("end")) break;
fileNameList.put(Integer.parseInt(incommingMessage.substring(incommingMessage.indexOf(".partN")+6)), incommingMessage);
}
File into=new File(fileNameList.get(0).substring(0,fileNameList.get(0).indexOf(".partN")-1));
try (FileOutputStream fos = new FileOutputStream(into);
BufferedOutputStream mergingStream = new BufferedOutputStream(fos)) {
for (String fileName : fileNameList.values()) {
Path p= Paths.get(fileName);
Files.copy(p, mergingStream);
Files.delete(p);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(fileNameList);
}
}