Last active
November 8, 2019 10:27
-
-
Save LuneFox/ae8113499cbfeb9e75454a82e536ad9c 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.task.task18.task1825; | |
import java.io.*; | |
import java.util.Map; | |
import java.util.TreeMap; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
/* | |
Собираем файл | |
*/ | |
public class Solution { | |
public static void main(String[] args) throws IOException { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
TreeMap<Integer, BufferedInputStream> streams = new TreeMap<>(); | |
BufferedOutputStream target = null; | |
String line; | |
while (!(line = reader.readLine()).equals("end")) { | |
Matcher matcher = Pattern.compile("(^.+)\\.part(\\d+)$").matcher(line); | |
if (matcher.find()) { | |
streams.put(Integer.parseInt(matcher.group(2)), new BufferedInputStream(new FileInputStream(line))); | |
if (target == null) target = new BufferedOutputStream(new FileOutputStream(matcher.group(1))); | |
} | |
} | |
reader.close(); | |
for (Map.Entry<Integer, BufferedInputStream> entry : streams.entrySet()) { | |
BufferedInputStream input = entry.getValue(); | |
byte[] buffer = new byte[input.available()]; | |
int count = input.read(buffer, 0, buffer.length); | |
input.close(); | |
target.write(buffer, 0, count); | |
} | |
assert target != null; | |
target.close(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment