Skip to content

Instantly share code, notes, and snippets.

@LuneFox
Last active November 8, 2019 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuneFox/ae8113499cbfeb9e75454a82e536ad9c to your computer and use it in GitHub Desktop.
Save LuneFox/ae8113499cbfeb9e75454a82e536ad9c to your computer and use it in GitHub Desktop.
Собираем файл
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