Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-11-01
6
Medium Priority
?
181 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 800 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 92

Accepted Solution

by:
objects earned 1200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

670 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