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

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
richardsimnettAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
agree with sciuriware, you'll find a good intro/example of using RAF here:

http://java.sun.com/developer/TechTips/2000/tt0509.html
0
 
CEHJCommented:
0
 
sciuriwareConnect With a Mentor Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
sciuriwareCommented:
To write the same 'k' after 1200 bytes:

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

;JOOP!
0
 
richardsimnettAuthor Commented:
Thanks!

0
 
sciuriwareCommented:
:)
0
All Courses

From novice to tech pro — start learning today.