Очень нравится что программа у меня работает коректно и все идет в нужный файл и в нужном количестве.
package com.javarush.task.task18.task1808;
import java.io.*;
import java.util.ArrayList;
/*
Разделение файла
*/
public class Solution {
public static BufferedReader userInput;
public static void main(String[] args) {
String filePath1 = userInputFilePath();
String filePath2 = userInputFilePath();
String filePath3 = userInputFilePath();
try {
userInput.close();
} catch (IOException e){
e.printStackTrace();
}
ArrayList<Integer> byteList = getAllByteFromFile(filePath1);
writeInFile(byteList, filePath2,filePath3);
}
private static String userInputFilePath(){
String filePath = "";
try {
userInput = new BufferedReader(new InputStreamReader(System.in));
filePath = userInput.readLine();
} catch (IOException e){
e.printStackTrace();
}
return filePath;
}
private static ArrayList<Integer> getAllByteFromFile(String filePath){
ArrayList<Integer> byteList = new ArrayList<>();
try (FileInputStream inputStream = new FileInputStream(filePath)){
while (inputStream.available() > 0){
byteList.add(inputStream.read());
}
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return byteList;
}
private static void writeInFile (ArrayList<Integer> byteList, String file2, String file3){
int oneBlock = 0;
if (byteList.size()%2 == 0){
oneBlock = byteList.size() - (byteList.size()/2);
writeInOneFiles(new ArrayList<>(byteList.subList(0,oneBlock)),file2);
writeInOneFiles(new ArrayList<>(byteList.subList(oneBlock,byteList.size())),file3);
} else {
oneBlock = (byteList.size() - (byteList.size()/2)) - 1;
writeInOneFiles(new ArrayList<>(byteList.subList(0,oneBlock)),file2);
writeInOneFiles(new ArrayList<>(byteList.subList(oneBlock,byteList.size())),file3);
}
}
private static void writeInOneFiles(ArrayList<Integer> byteList,String pathFile){
try (FileOutputStream outputStream = new FileOutputStream(pathFile)){
for (Integer cell : byteList){
outputStream.write(cell);
}
} catch (IOException e){
e.printStackTrace();
}
}
}