Sample code for random access file io program

Discussion in 'Java' started by GreenMountainBoy, Nov 10, 2006.

  1. 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
     
    GreenMountainBoy, Nov 10, 2006
    #1
    1. Advertising

  2. GreenMountainBoy

    Ian Wilson Guest

    GreenMountainBoy wrote:
    >
    > 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".
     
    Ian Wilson, Nov 10, 2006
    #2
    1. Advertising

  3. GreenMountainBoy

    Guest

    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



    GreenMountainBoy wrote:
    > 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
     
    , Nov 10, 2006
    #3
  4. Thanks - I just got over eager.... any ideas?

    Ian Wilson wrote:
    > GreenMountainBoy wrote:
    > >
    > > 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, Nov 10, 2006
    #4
  5. GreenMountainBoy wrote:
    > Ian Wilson wrote:
    >
    >>GreenMountainBoy wrote:
    >>
    >>>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.
    >>

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


    Yes - see my reply to your question in comp.lang.java.help.
     
    RedGrittyBrick, Nov 10, 2006
    #5
  6. GreenMountainBoy

    Tom Forsmo Guest

    GreenMountainBoy wrote:
    > 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
     
    Tom Forsmo, Nov 16, 2006
    #6
  7. GreenMountainBoy

    Ingo Menger Guest

    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.
     
    Ingo Menger, Nov 16, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Chase
    Replies:
    0
    Views:
    100
    Tim Chase
    Feb 16, 2014
  2. Terry Reedy
    Replies:
    0
    Views:
    108
    Terry Reedy
    Feb 16, 2014
  3. Tim Chase
    Replies:
    0
    Views:
    102
    Tim Chase
    Feb 16, 2014
  4. Ned Batchelder
    Replies:
    0
    Views:
    103
    Ned Batchelder
    Feb 16, 2014
  5. duncan smith
    Replies:
    0
    Views:
    101
    duncan smith
    Feb 16, 2014
Loading...

Share This Page