I can't see what I am doing wrong, please help!
package com.codegym.task.task18.task1808;
/*
Splitting a file
*/
import java.io.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String file1 = br.readLine();
String file2 = br.readLine();
String file3 = br.readLine();
br.close();
FileInputStream fileInputStream = new FileInputStream(file1);
int totalBytes = fileInputStream.available();
int file3FilesSize = totalBytes / 2;
int file2FilesSize = totalBytes / 2 + totalBytes % 2;
int file4FileSizeWhenEqual = totalBytes / 2;
byte[] largerHalfSize = new byte[file2FilesSize];
byte[] smallerHalfSize = new byte[file3FilesSize];
byte[] equalHalfSize = new byte[file4FileSizeWhenEqual];
if (totalBytes % 2 != 0) {
//here i am reading the first larger half of the file and putting it into the array
for (int i = 0; i < fileInputStream.available() - file3FilesSize; i++) {
largerHalfSize[i] = (byte) fileInputStream.read();
}
//here i am reading the second smaller half of the file and putting it into the array
for (int i = fileInputStream.available() - file3FilesSize; i < fileInputStream.available() ; i++) {
smallerHalfSize[i] = (byte) fileInputStream.read();
}
//this writes the content of largerhalfsize into file2 but largerhalfsize is an array with no values yet.
FileOutputStream fileOutputStream = new FileOutputStream(file2);
fileOutputStream.write(largerHalfSize);
FileOutputStream fileOutputStream2 = new FileOutputStream(file3);
fileOutputStream2.write(smallerHalfSize);
fileInputStream.close();
fileOutputStream.close();
fileOutputStream2.close();
} else {
//here i am reading the first half in case the size is an even number
for (int i = 0; i < fileInputStream.available() / 2; i++) {
equalHalfSize[i] = (byte) fileInputStream.read();
}
//here i am reading the second half in case the size is an even number
for (int i = fileInputStream.available() / 2; i < fileInputStream.available() ; i++) {
smallerHalfSize[i] = (byte) fileInputStream.read();
}
FileOutputStream fileOutputStream3 = new FileOutputStream(file2);
fileOutputStream3.write(largerHalfSize);
FileOutputStream fileOutputStream4 = new FileOutputStream(file3);
fileOutputStream4.write(smallerHalfSize);
fileInputStream.close();
fileOutputStream3.close();
fileOutputStream4.close();
}
/* write(byte[] b)
Writes b.length bytes from the specified byte array to this file output stream.
*/
/*
I need to read from file1 the first half into file 2 (so the larger half if applicable)
and the second half (the smaller half) into file 3
*/
}
}