Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Sample code for random access file io program

Thread view: 
GreenMountainBoy - 10 Nov 2006 15:58 GMT
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
Ian Wilson - 10 Nov 2006 16:27 GMT
> 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".
GreenMountainBoy - 10 Nov 2006 19:42 GMT
Thanks - I just got over eager.... any ideas?

> > p.s. I posted this on the java help forum but it seems that it requires
> > the expertise that this forum provides.
[quoted text clipped - 5 lines]
> difference hard to spot but Google is just a front-end to newsgroups and
> to Google's own "forums".
RedGrittyBrick - 10 Nov 2006 20:08 GMT
>>>p.s. I posted this on the java help forum but it seems that it requires
>>>the expertise that this forum provides.
[quoted text clipped - 3 lines]
>>
> Thanks - I just got over eager.... any ideas?

Yes - see my reply to your question in comp.lang.java.help.
lborupj@gmail.com - 10 Nov 2006 17:38 GMT
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

> HELLO,
>
[quoted text clipped - 36 lines]
>
> thanks, GreenMountainBoy
Tom Forsmo - 16 Nov 2006 11:55 GMT
> The record structure is
>
[quoted text clipped - 5 lines]
>     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
Ingo Menger - 16 Nov 2006 12:25 GMT
GreenMountainBoy schrieb:

> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.