В чем дело ? Прога работает, но ее валидация идет слишком долго и заканчивается ошибкой с сообщением "Программа работала слишком долго и была закрыта!". При этом я тестировал ее на файле размером около 50 Мб и с таким файлом она выполняется не более секунды. Это вторая версия решения, в первой для хранения содержимого файлов я юзал List'ы, а не массивы байт. В версии с List'ами валидация проходила успешно, но говорила, что какие-то из условий задачи не выполнены. Якобы потоки не закрыты. Поэтому решил переписать, но стало еще хуже.
package com.javarush.task.task18.task1808;
/*
Разделение файла
*/
import java.io.*;
public class Solution {
public static void main(String[] args) {
// считываем три имени файла с консоли
String fileName1 = "";
String fileName2 = "";
String fileName3 = "";
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
fileName1 = reader.readLine();
fileName2 = reader.readLine();
fileName3 = reader.readLine();
} catch (IOException e) {
}
// считываем содержимое file1 в массив байт
byte[] file1Bytes = new byte[]{};
try (FileInputStream input = new FileInputStream(fileName1)) {
ByteArrayOutputStream byteArrStream = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int count;
while ((count = input.read(buf)) != -1) {
byteArrStream.write(buf, 0, count);
}
file1Bytes = byteArrStream.toByteArray();
} catch (FileNotFoundException e) {
System.out.println("FNFE");
} catch (IOException e) {
}
// чекаем, четное или нечетное количество байт в file1
boolean file1Even = file1Bytes.length % 2 == 0;
// определяем содержимое file2 и file3
byte[] file2Bytes;
byte[] file3Bytes;
if (file1Even) {
int arr2length = file1Bytes.length / 2;
int arr3length = file1Bytes.length / 2;
file2Bytes = new byte[arr2length];
System.arraycopy(file1Bytes, 0, file2Bytes, 0, arr2length);
file3Bytes = new byte[arr3length];
System.arraycopy(file1Bytes, arr3length, file3Bytes, 0, arr3length);
} else {
int arr2length = file1Bytes.length / 2 + 1;
file2Bytes = new byte[arr2length];
System.arraycopy(file1Bytes, 0, file2Bytes, 0, arr2length);
int arr3length = file1Bytes.length / 2;
file3Bytes = new byte[arr3length];
System.arraycopy(file1Bytes, arr3length, file3Bytes, 0, arr3length);
}
// пишем в файл2
try (FileOutputStream output2 = new FileOutputStream(fileName2)) {
ByteArrayInputStream byteArrStream = new ByteArrayInputStream(file2Bytes);
byte[] buf = new byte[1024];
int count;
while ((count = byteArrStream.read(buf)) != -1) {
output2.write(buf, 0, count);
}
} catch (FileNotFoundException e) {
System.out.println("FNFE");
} catch (IOException e) {
System.out.println("IOE");
}
// пишем в файл3
try (FileOutputStream output3 = new FileOutputStream(fileName3)) {
ByteArrayInputStream byteArrStream = new ByteArrayInputStream(file3Bytes);
byte[] buf = new byte[1024];
int count;
while ((count = byteArrStream.read(buf)) != -1) {
output3.write(buf, 0, count);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}