Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8606971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:13:33+00:00 2026-06-12T03:13:33+00:00

Possible Duplicate: Best Way to Write Bytes in the Middle of a File in

  • 0

Possible Duplicate:
Best Way to Write Bytes in the Middle of a File in Java

I have a file in which I need to write bytes.

I know at which position in the file I need to insert specific bytes. To make things clear, I need to write bytes in the middle of the file without erasing any existing bytes. The whole operation should then increase the length of the file.

What is the best way to do so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:13:35+00:00Added an answer on June 12, 2026 at 3:13 am

    The only way to do this is to movethe bytes that are currently in the way. Depending on how frequently you have to do this, and how large the file is, it’s often a better idea to create a new file, copying the existing file and inserting the new bytes along the way.

    If you need to update the file infrequently, or it’s small (up to maybe 100 kb) you can use a RandomAccessFile:

    1. Extend the file, using the setLength() method, adding the number of bytes you’ll be inserting to whatever is returned by the length() method.
    2. If you have enough memory, create a byte[] that will hold all the bytes from the insertion point to the previous end of file.
    3. Call seek() to position at the insertion point
    4. Call readFully() to fill your temporary array
    5. Call seek() to position at the insertion point + the number of bytes to insert
    6. Call write() to write your buffer at that point
    7. Call seek() to reposition at the insertion point
    8. Call `write() to write the new bytes

    If you can’t create a large-enough array in step #2, you’ll have to perform steps 3-6 in a loop with a smaller buffer. I would use at least a 64k buffer for efficiency.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Best way to get identity of inserted row? I have an INSERT
Possible Duplicate: Best way to stop SQL Injection in PHP I have seen some
Possible Duplicate: Best way to stop SQL Injection in PHP I need to secure
Possible Duplicate: ASP.Net:Best way to run scheduled tasks I have to make a scheduled
Possible Duplicate: Best way to detect integer overflow in C/C++ i have tried to
Possible Duplicate: Best Way to Sprite Images? Hi i have been using this http://spritegen.website-performance.org
Possible Duplicate: Best way to periodically execute a PHP script? I need to run
Possible Duplicate: In Java, is there a way to write a string literal without
Possible Duplicate: What is the best way to convert a java object to xml
Possible Duplicate: Best Way to Sprite Images? I have the following image that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.