-2

How can I split a file in two ( file1 and file2 ) such that the file1 contains first 10kb of the file and file2 contains rest of the remaining data of the file. I am using AIDE on android.

Chrishdev
  • 55
  • 7

1 Answers1

0

There is no "system call" to split a file. You need to open a file, read it and copy the contents to the corresponding output files (which you need to create).

Synopsis:

  • Open the input file as a FileInputStream
  • Make a byte[] buffer somewhere around 4k
  • Open the two output files as two FileOutputStreams
  • Read from input into buffer and write buffer to first OutputStream
  • Do this until exactly 10kb bytes have been read and written
  • Read from input into buffer and write buffer to second OutputStream
  • Do this until there are no more bytes from the input stream
  • Close all three streams

Of course, you will need to be careful to make sure that you copy exactly the correct number of bytes. See InputStream.read(buf, offset, length) for details. Test also for special case when input file is less than 10k long.

jurez
  • 4,436
  • 2
  • 12
  • 20