В новый файл перепиши все байты из файлов-частей *.partN.
Чтение и запись должны происходить с использованием буфера.
Не все потоки чтения из файла были закрыты.
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
Set<String> files = new TreeSet<>((o1, o2) -> {
String[] parts1 = o1.split(".part");
String[] parts2 = o1.split(".part");
int i1 = Integer.parseInt(parts1[parts1.length - 1]);
int i2 = Integer.parseInt(parts2[parts2.length - 1]);
return i1 - i2;
});
String fileOutput = null;
String tempRead;
while (!(tempRead = read.readLine()).equals("end")) {
files.add(tempRead);
if (fileOutput == null) {
int index = tempRead.lastIndexOf(".part");
fileOutput = tempRead.substring(0,index );
}
}
if (fileOutput == null) {
return;
}
try (FileOutputStream out = new FileOutputStream(fileOutput)) {
for (String file : files) {
try (FileInputStream in = new FileInputStream(file)) {
byte[] buffer = new byte[in.available()];
while (in.available() > 0) {
int readCount = in.read(buffer);
out.write(buffer, 0, readCount);
}
}
}
}
read.close();
}
}
!!!!!!!!!!!!!!!!!!!!!!!!! Вот правильное решение только здесь мне кажиться ошибка, так как в правильнои решении не закрываеться поток BufferedReader
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Set<String> files = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String[] parts1 = o1.split(".part");
String[] parts2 = o2.split(".part");
int number1 = Integer.parseInt(parts1[parts1.length - 1]);
int number2 = Integer.parseInt(parts2[parts2.length - 1]);
return number1 - number2;
}
});
String outputFile = null;
String readString;
while (!(readString = reader.readLine()).equals("end")) {
files.add(readString);
if (outputFile == null) {
int indexOfSuffix = readString.lastIndexOf(".part");
outputFile = readString.substring(0, indexOfSuffix);
}
}
if (outputFile == null) return;
try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile)) {
for (String file : files) {
try (FileInputStream fileInputStream = new FileInputStream(file)) {
byte[] buffer = new byte[fileInputStream.available()];
while (fileInputStream.available() > 0) {
int bytesRead = fileInputStream.read(buffer);
fileOutputStream.write(buffer, 0, bytesRead);
}
}
}
}
}
}
package com.javarush.task.task18.task1825;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
/*
Собираем файл
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
Set<String> files = new TreeSet<>((o1, o2) -> {
String[] parts1 = o1.split(".part");
String[] parts2 = o1.split(".part");
int i1 = Integer.parseInt(parts1[parts1.length - 1]);
int i2 = Integer.parseInt(parts2[parts2.length - 1]);
return i1 - i2;
});
String fileOutput = null;
String tempRead;
while (!(tempRead = read.readLine()).equals("end")) {
files.add(tempRead);
if (fileOutput == null) {
int index = tempRead.lastIndexOf(".part");
fileOutput = tempRead.substring(0,index );
}
}
if (fileOutput == null) {
return;
}
try (FileOutputStream out = new FileOutputStream(fileOutput)) {
for (String file : files) {
try (FileInputStream in = new FileInputStream(file)) {
byte[] buffer = new byte[in.available()];
while (in.available() > 0) {
int readCount = in.read(buffer);
out.write(buffer, 0, readCount);
}
}
}
}
read.close();
}
}