The lessons are teaching me less and less. I've failed this task like half a dozen times already.
So I check if my count is even or odd.
If odd (count % 2 != 0), I write from 0 the first count / 2 characters. So, if I had 7 bytes, 7 / 2 should give me 3. 0-3.
Then I write starting at count / 2 (or 3) to count/2 + 1, which should be 4 additional characters. 3 + 4 = 7, and the larger half is in the second file.
What am I missing?
package com.codegym.task.task18.task1808;
/*
Splitting a file
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String file1 = reader.readLine();
String file2 = reader.readLine();
String file3 = reader.readLine();
reader.close();
FileInputStream inputStream = new FileInputStream(file1);
FileOutputStream outputStream = new FileOutputStream(file2);
FileOutputStream outputStream1 = new FileOutputStream(file3);
byte[] buffer = new byte[inputStream.available()];
while (inputStream.available() > 0) {
int count = inputStream.read(buffer);
if (count % 2 != 0) {
outputStream.write(buffer, 0, (count / 2)-1);
outputStream1.write(buffer, (count / 2) -1, (count / 2) + 1);
}
else {
outputStream.write(buffer, 0, count / 2);
outputStream1.write(buffer, count / 2, count / 2);
}
}
inputStream.close();
outputStream.close();
outputStream1.close();
}
}