Solved

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

Posted on 2006-11-01
6
174 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 54
ForLoop Example 3 47
egit plugin on eclipse 8 62
SHA2 certs for IIS AND Java? 2 77
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

815 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

13 Experts available now in Live!

Get 1:1 Help Now