-1

How to write bytes in the middle (specific position) of a file in android or java without overwriting any data?

Example If I have file of size 500kb, I wish to add 512 bytes of data after 200kb

This question has been marked as duplicate but the referred question doesn't have an answer.

Shreyas Tripathy
  • 325
  • 7
  • 19
  • See [this](http://stackoverflow.com/questions/181408/best-way-to-write-bytes-in-the-middle-of-a-file-in-java) – Skizo-ozᴉʞS ツ Feb 11 '15 at 11:09
  • Define file. Is it a text file that you wish to add some characters or a raw binary file? – George Daramouskas Feb 11 '15 at 11:09
  • @JoanColmenero: but this will overwrite file content, which the OP does not want – cello Feb 11 '15 at 11:11
  • @GeorgeD - It could be of any format. The idea behind this is to add security. Adding _garbage_ data while uploading the file so that only a selected application can open the file. The selected application would remove those _garbage_ bytes in order to make the file accessible – Shreyas Tripathy Feb 11 '15 at 11:15
  • @JoanColmenero - I saw that and just like cello mentioned, it would overwrite my data. – Shreyas Tripathy Feb 11 '15 at 11:19
  • @ShreyasTripathy First, there's no security to gain here. So if you want security, use something else. If you *still* want to insert garbage between the server and client, you could just write the first 200 kb of the file, then write 512 b garbage to the server stream, then continue to write the file from there. No need to modify the original file on the client for this. – Harald K Feb 11 '15 at 12:18
  • @haraldK - I get that. The thing is, if I insert the garbage at a specific point in the middle, only I would know what to remove and how much to remove. That's the security. Adding in the beginning would cause the same issue I was having before. – Shreyas Tripathy Feb 11 '15 at 12:42
  • Do you think "This is SLIGHTLY LESS insecure" is much harder to read than "This is insecure"? ;-) Yes, you make it harder to open the file for people who have no clue. But people with a clue will figure out what's going on. In any case, it's your call. I'll leave you to it. – Harald K Feb 11 '15 at 12:47

1 Answers1

1

The simple answer: you can't. you can skip to 200kb, but then you need to read the rest of the file, e.g. in a ByteBuffer, go back to 200kb-position, write your 512 bytes and then write the 300kb from the ByteBuffer again. By design, file content cannot be "shifted", only read and (over-)written, so you have to do the shifting yourself.

cello
  • 5,356
  • 3
  • 23
  • 28
  • What about `RandomAccessFile` ? Does that let me **seek** to a specific position in the file? – Shreyas Tripathy Feb 11 '15 at 11:17
  • @ShreyasTripathy Yes, but it doesn't solve your problem. While seek can potentially be faster than skip, you still need to copy the remaining data forward. – Harald K Feb 11 '15 at 11:22
  • @haraldK - What about the solution present [here](http://stackoverflow.com/questions/12677170/write-bytes-into-a-file-without-erasing-existing-bytes?lq=1) ? – Shreyas Tripathy Feb 11 '15 at 11:23
  • @ShreyasTripathy I think that is what cello tried to tell you... – Harald K Feb 11 '15 at 11:28
  • @haraldK - Okay, I had a question about that. I am dealing with various file types here (videos, pdf, images, etc.). Would there be enough buffer size available in case of a video file? – Shreyas Tripathy Feb 11 '15 at 11:32