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.
Asked
Active
Viewed 138 times
-2
-
I see a link, hope it helps: https://stackoverflow.com/questions/31179273/splitting-and-merging-large-files-size-in-gb-in-java – Vinh Can Code Jul 29 '18 at 10:14
-
As with all programs, by writing the code that you want. If you’re stuck, post a [mcve]. – Erwin Bolwidt Jul 29 '18 at 10:28
1 Answers
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