Sample code for random access file io program

G

GreenMountainBoy

HELLO,


Would anyone be able to advise me as to how to write a simple Random
Access File I/O program.


The record structure is

public Employee

{
int iRecordNumber;

String sName;

double dSalary;

}


I need to be able to create these records from the keyboard, then write
them to disk, and then be able to read one based on a iRecordNumber.
Part of the problem is that the Name string is variable length and I
need it to be a fixed 25 characters - I have found that character
arrays won't function in many places where a string is called for..any
simple solutions? I guess I could pad the string so that it is the
right length, but the rest of the program still confuses me. any help
would be forever remebered.


I need to be able to 1) add new items, 2) list all items, 3) query an
item based on its record number, 4) update a record (salary only), 5)
delete an item.


I have looked in all the standard Java texts for an example framework,
but there is nothing comparable....

THANKS SO MUCH FOR YOUR HELP... I REALLY APPRECIATE IT...

p.s. I posted this on the java help forum but it seems that it requires
the expertise that this forum provides.


thanks, GreenMountainBoy
 
I

Ian Wilson

GreenMountainBoy said:
p.s. I posted this on the java help forum but it seems that it requires
the expertise that this forum provides.

You have to wait more than 10 mins for an answer!
I usually allow up to 48 hours for an answer in a newsgroup.

P.S. newsgroups are not the same as web-based forums. Google makes the
difference hard to spot but Google is just a front-end to newsgroups and
to Google's own "forums".
 
L

lborupj

Actually you might find yourself writing a mini database system for
this to work
really well and fast enough for lookups and such, so a better approach
would
probably be to include hsql or a similar embeddable database in your
project.. Its
quite simple.
BUT if this is not an option I would suggest you make your record size
fixed and
drop the iRecordNumber in the structure. Then you have to write some
code which
get an exclusive lock on a fil, lets call it data.dat. Seek to last
position in file for inserts,
seek to position recordSize * iRecordNumber (index) for reads and
updates and use
a mechanism to mark a record as deleted if this is necessary. Then
create a sweeper
which goes thru the file once in a while, copying records to a new
file, but leaving the deleted once to clean things up.

regards, Lars Borup Jensen
 
G

GreenMountainBoy

Thanks - I just got over eager.... any ideas?

Ian said:
You have to wait more than 10 mins for an answer!
I usually allow up to 48 hours for an answer in a newsgroup.

P.S. newsgroups are not the same as web-based forums. Google makes the
difference hard to spot but Google is just a front-end to newsgroups and
to Google's own "forums".
 
T

Tom Forsmo

GreenMountainBoy said:
The record structure is

public Employee

{
int iRecordNumber;
String sName;
double dSalary;
}

First a quick question, are you absolutely sure it needs to be record
based? Could it not be textbased? this is much easier in java than
records E.g.

recordnum=1024
sname=xxxxxx xxxxxxxx
salary=3.14

In any case, what you probably should do is use a byte array, set up for
the exact size of the record, then for each employee oject you add a
method that returns a byte array representation of the object. then you
use a list iterator to iterate through the list and give you each byte
array representation in the list.

e.g. record format in BigEndian format, with \0 based padding

num (4 bytes) | sname (25 bytes) | salary (8 bytes)


public interface Recordformat {
public byte[] toRecord();
}

public class Employee implements RecordFormat {
...
public byte[] toRecord() {
}


I need to be able to create these records from the keyboard, then write
them to disk, and then be able to read one based on a iRecordNumber.
Part of the problem is that the Name string is variable length and I
need it to be a fixed 25 characters - I have found that character
arrays won't function in many places where a string is called for..

Why is that?
I need to be able to 1) add new items, 2) list all items, 3) query an
item based on its record number, 4) update a record (salary only), 5)
delete an item.

When it comes to the file access you could use java.io.RandomAccessFile
to make a filemanager object which traverses the file in record size
chunks, both backwards and forwards, and allows you to save both single
records and a list of records. You can delete a record by \0 padding it,
or just rewrite the entire file with the updated list.


tom
 
I

Ingo Menger

GreenMountainBoy said:
HELLO,


Would anyone be able to advise me as to how to write a simple Random
Access File I/O program.


The record structure is
[irrelevant]

How much data do you expect?
If its not too much, I'd read all items from an Object Input Stream,
place them in an appropriate Conatiner and write them back at the end
(or in between on user request, maybe).
For a few megabytes this should be no problem.
I need to be able to 1) add new items, 2) list all items, 3) query an
item based on its record number, 4) update a record (salary only), 5)
delete an item.

The advantages of the solution proposed by me are:
- use of standard technologies (Serialization, etc.) and classes.
- fast, once data are loaded.
- no severe problems with accidental parallel access of "database", the
worst thing that may happen is that some changes get lost.

However, if you need parallel access (i.e. more than one program using
that data may be running at the same time) I strongly advise to use the
technology that has been invented some decades ago for such cases: use
a relational database.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top