Task:

It is necessary to divide the file into 2 files exactly if in the main file there is an odd number of bytes, then in 1 file write 1 more bytes.

I just can not figure out how to make the separation so that it is recorded

public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); FileInputStream in = new FileInputStream("e:/name.txt"); FileOutputStream out = new FileOutputStream("e:/name1.txt"); FileOutputStream out2 = new FileOutputStream("e:/name2.txt"); ArrayList<Integer> list = new ArrayList<>(); while (in.available()>0){ list.add(in.read()); } int count = 0; if (list.size()%2==0) { for (int i = 0; i < list.size()/2; i++) { int data = in.read(); out.write(data); count++; } } 
  • What's wrong with this code? - default locale

1 answer 1

 File file = new File("e:/name.txt"); FileInputStream in = new FileInputStream(file); long length = file.length(); long part1Length = length / 2 + length % 2; FileOutputStream out = new FileOutputStream("e:/name1.txt"); FileOutputStream out2 = new FileOutputStream("e:/name2.txt"); long read = 0; int b; while ((b = in.read()) >= 0){ if(++read <= part1Length) out.write(b); else out2.write(b); } out.close(); out2.close(); 

PS method available not intended to determine the end of the file, read javadoc to it.