Solved

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

Posted on 2006-11-01
6
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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 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

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 …
The viewer will learn how to implement Singleton Design Pattern in Java.

710 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