[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Updating a segment of binary file

Posted on 2006-05-11
7
Medium Priority
?
245 Views
Last Modified: 2010-03-31
Hello -

I have a binary file that I create using Java's output stream. The file is made up of two parts - a header (10 bytes max) that describes the file's remaining contents and then the content itself. The content represents a user's Phone Number. New phone numbers could be added towards the end of the file.

The header contains the last phone number that was added. Hence the first 10 bytes representing the header need to be updated whenever a new phone number is added in order to indicate the last number.

How can I use Java to update only the first 10 bytes (i.e., the header)?
The header is guaranteed to be only 10 bytes.

If I use Java to 'read' the first 10 bytes, then how can I 'write' these to the first - i.e, replace the first 10 bytes?

Example code will increase the points allotted for this question. Thanks in advance!
0
Comment
Question by:baigmz
  • 3
  • 3
7 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 16664434
You can read the first ten bytes using this:

http://javaalmanac.com/egs/java.io/File2ByteArray.html

Keep the array in memory, modify the contents. Then read the remaining from the file into the same array. After that, you can write the updated array back to the same file using FileOutputStream.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 16664448
0
 
LVL 9

Expert Comment

by:Manikandan Thiagarajan
ID: 16664449
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 16664468
I was about to suggest RAF but it might need more code modifications ;-) anyway, if you think you're fine with it, then its an easier approach.
0
 
LVL 9

Expert Comment

by:Manikandan Thiagarajan
ID: 16664524
i also posted the same comment at same time
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16664540
Yes, but the one that appears first gets the points perhaps because he posted it a few seconds earlier :-)
0
 
LVL 9

Expert Comment

by:Manikandan Thiagarajan
ID: 16664562
no problem
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question