Site navigation: [ Home | Theory | Java | About ]

Random Access Files

OK, after many requests, here it is.

From records to RAF files.

There will be gaps and these are "learning opportunities" :-)

Let the great journey commence...


Getting the size of your record (in bytes) correct is vital because you need to be able to seek to the start of a new record using this figure in the calculation.

Most problems occur when this is incorrectly calculated.







Good to check the data entered when creating a new instance.





Trivial example of polymorphism and anyway, you could find a much better way to display rcords from the file.


A record structure - we will need to define this first. If you are to convince the examiner that you know your way around this topic, we strongly suggest you have different data types in your records.;

This record structure is about club members or something similar

* Stores data about club members
* @author Mr J
* @version 20050322
public class M
  // need to know the size, in bytes of the records so we can store
  // them in a Random Access file

  public static final int CODE_L = Integer.SIZE / 8; // bytes in an int
  public static final int FIRST_L = 20; // max size for a first name
  public static final int LAST_L = 25;  // max size for a last name
  public static final int PAID_L = 1;   // size to store a boolean primitive
  // Multiply char data by 2 because 16-bit UNICODE chars
  // are written to the file - number of bytes per record
  public static final int RECORD_LENGTH = CODE_L + (FIRST_L + LAST_L) * 2
                                        + PAID_L;
  // data fields
  private int code;
  private String first;
  private String last;
  private boolean paid;
  * Constructor for objects of class Member
  public Member(int code, String first, String last, boolean paid)
  // Accessor & mutator methods - need validation checks here
  public void setCode(int code){ this.code = code; }
  public void setFirst(String first){ this.first = first; }
  public void setLast(String last){ this.last = last; }
  public void setPaid(boolean paid) { this.paid = paid; }
  public int getCode(){ return this.code; }
  public String getFirst(){ return this.first; }
  public String getLast(){ return this.last; }
  public boolean hasPaid(){ return this.paid; }

  // A String representation of the record. Overrides method
  // inherited from Object. Is this polymorphism then? Yes, Jim,
  // but not as IB Mastery Aspects know it.

  public String toString()
    return (code + ": " + last.trim() + ", " + first.trim()
                 + " paid? " + paid);

Related: [ Java home | Files home | Previous:serialization | Next: Console ]

Now, this is a teaching resource, rather than a series of code examples to copy without understanding, so you will have to work on these to understand them!

The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: October 28, 2013