Checked with multiple files, seems to work on all occasions.
I decided not to use the while (available >0) loop as it is not needed when writing everything in the array at once.
The requirement failures seem to indicate the validation process is expecting something.
Would be good to know if my code is bad or the validation is too strict.
Thank you!
package com.codegym.task.task18.task1808;
/*
Splitting a file
*/
import java.io.*;
public class Solution {
public static void main(String[] args) throws IOException {
//Requirements 1,2 --> start initialization using File streams
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String file1 = r.readLine();
String file2 = r.readLine();
String file3 = r.readLine();
FileInputStream in = new FileInputStream(file1);
FileOutputStream out1 = new FileOutputStream(file2);
FileOutputStream out2 = new FileOutputStream(file3);
int half;
byte[] buffer1;
byte[] buffer2;
// end init
// Preparing buffers for input
if (in.available() % 2 != 0) {
half = (in.available() / 2) + 1;
buffer1 = new byte[half];
buffer2 = new byte[half - 1];
} else {
half = in.available() / 2;
buffer1 = new byte[half];
buffer2 = new byte[half];
}
// 3,4 reading and writing halves of bytes
boolean moveOn = false;
while (!moveOn) {
in.read(buffer1);
if (buffer1[half - 1] != 0) ;
moveOn = true;
}
in.read(buffer2);
out1.write(buffer1);
out2.write(buffer2);
// end 3,4
//5 close streams
in.close();
out1.close();
out2.close();
}
}