Solved

How do I create, read and write binary records to a file?

Posted on 2006-11-01
6
172 Views
Last Modified: 2010-03-31
Hello,
This question is kinda basic, but it is something I cannot find any reference to do. I want to be able to create a record of a fixed size. Two Long integers stored in a record / class of some sort. Then I need to be able to write this data record by record to a binary file. I also need to know how to read this binary data back out of the file.

Please provide code examples.

Worth 500 points.

Rick
0
Comment
Question by:richardsimnett
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17849462
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 200 total points
ID: 17849465
Use RandomAccessFile:

  RandomAccessFile f = new RandomAccessFile(new File("myfile"), "rw");
  long[] k = new long[2];

  k[0] = f.readLong();
  k[1] = f.readLong();
 
;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17849482
To write the same 'k' after 1200 bytes:

    f.seek(1200L);
    f.writeLong(k[0]);
    f.writeLong(k[1]);

;JOOP!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 17853072
agree with sciuriware, you'll find a good intro/example of using RAF here:

http://java.sun.com/developer/TechTips/2000/tt0509.html
0
 

Author Comment

by:richardsimnett
ID: 17881432
Thanks!

0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17881469
:)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now