Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java Lesson 46: Using the RandomAccessFile class

JavaFAQ Home » Java Lessons by Jon Huhtala Go to all tips in Java Lessons by Jon Huhtala


Bookmark and Share
All Java Lessons contents page | Java Lesson 1 | Java Lesson 2 | Java Lesson 3 | Java Lesson 4 | Java Lesson 5 | Java Lesson 6 | Java Lesson 7 | Java Lesson 8 | Java Lesson 9 | Java Lesson 10 | Java Lesson 11 | Java Lesson 12 | Java Lesson 13 | Java Lesson 14 | Java Lesson 15 | Java Lesson 16 | Java Lesson 17 | Java Lesson 18 | Java Lesson 19 | Java Lesson 20 | Java Lesson 21 | Java Lesson 22 | Java Lesson 23 | Java Lesson 24 | Java Lesson 25 | Java Lesson 26 | Java Lesson 27 | Java Lesson 28 | Java Lesson 29 | Java Lesson 30 | Java Lesson 31 | Java Lesson 32 | Java Lesson 33 | Java Lesson 34 | Java Lesson 35 | Java Lesson 36 | Java Lesson 37 | Java Lesson 38 | Java Lesson 39 | Java Lesson 40 | Java Lesson 41 | Java Lesson 42 | Java Lesson 43 | Java Lesson 44 | Java Lesson 45 | Java Lesson 46

Java Lesson 46 by Jon Huhtala

Using the RandomAccessFile class


Overview

In the previous lesson, you learned how to uses Java's I/O streams to write data to disk and read data from disk. In both cases, the organization of the file was sequential. All file data was written or read one item after another.

Sequential files are useful, but they do not provide a way to quickly locate an item of data without reading through all the other items of data that precede it. They also do not provide a means of updating an item of data without copying the entire file.

In this lesson, you will learn the essentials of Java's RandomAccessFile class, which permits a program to directly access an item of data and replace its contents.

........
Continues on the next page, click link below...

The RandomAccessFile class

  • Is part of the java.io package

  • Is an extension of the Object class. It does NOT descend from the InputStream or OutputStream classes.

Object

RandomAccessFile
  • Is a high-level class that can be used to directly read or write primitive data items and UTF strings to a file. The file behaves like a large array of bytes. By positioning a pointer ("seeking") to a byte location within the file, a particular item of data may be either read or written.

  • Has two constructors. The most frequently used requires two parameters
  1. The reference of a File object that contains the pathname of the file.

  2. A String that represents the mode of access. It must be either "r" (read only) or "rw" (read/write).

For example, if fd is the reference of a File object

RandomAccessFile raf = new RandomAccessFile(fd, "rw");

will construct a RandomAccessFile object for both reading and writing primitive values and UTF strings to the file. Because a checked, IOException may occur, the statement should be enclosed in a try block with an appropriate catch.
  • Has many useful methods as follows:

Method

Usage

getFilePointer()

Returns the byte offset of the pointer into this file

length()

Returns the byte length of this file

readBoolean()

Reads a boolean value from the pointer location within the file

readByte()

Reads a byte value from the pointer location within the file

readChar()

Reads a char value from the pointer location within the file

readDouble()

Reads a double value from the pointer location within the file

readFloat()

Reads a float value from the pointer location within the file

readInt()

Reads a int value from the pointer location within the file

readLong()

Reads a long value from the pointer location within the file

readShort()

Reads a short value from the pointer location within the file

readUTF()

Reads a String from the pointer location within the file according to the UTF standard

seek()

Sets the pointer offset, measured from the beginning of this file, at which the next read or write will occur

writeBoolean()

Writes a specified boolean value to the pointer location within the file

writeByte()

Writes a specified byte value to the pointer location within the file

writeChar()

Writes a specified char value to the pointer location within the file

writeDouble()

Writes a specified double value to the pointer location within the file

writeFloat()

Writes a specified float value to the pointer location within the file

writeInt()

Writes a specified int value to the pointer location within the file

writeLong()

Writes a specified long value to the pointer location within the file

writeShort()

Writes a specified short value to the pointer location within the file

writeUTF()

Writes a specified String to the pointer location within the file according to the UTF standard

Notes:

  1. All read and write methods automatically advance the pointer to the next data item within the file.

  2. Because a checked, IOException may occur, calls to these methods should be enclosed in a try block with an appropriate catch. Consult the Java API documentation for more details.

  • Example. The following program creates a file of 100 integers. The user is then allowed to directly read and change the value of selected integers within the file. The final contents of the file are then displayed.

import java.io.*;
public class App {
public static void main(String[] args) {

// Local variables and object references.

File fd;

// Get the path name from the user.

System.out.print("Enter the file's complete path name: ");
fd = new File(Keyboard.readString());

// Try to process the file.

try {

// Format the file with 100 integer values (all zero).

DataOutputStream out = new DataOutputStream(new FileOutputStream(fd));
for (int i = 0; i < 100; i++) {
out.writeInt(0);
}
out.close();
System.out.println("The file is formatted with 100 integers");

// Re-open the file for random reading and writing.

RandomAccessFile raf = new RandomAccessFile(fd, "rw");

// Allow the user to see and change any integer value within
// the file.

char again;
do {

// Ask the user for the integer's position within the file.

System.out.print("Specify an integer's position (0 to 99): ");
int position = Keyboard.readInt();

// Calculate the integer's offset (byte displacement) within
// the file.

int pointer = position * 4;

// If the offset is valid, locate the integer, read and display
// its current value, get a new value from the user, and write
// the new value back to the same location within the file.

if (pointer < raf.length()) {
raf.seek(pointer);
System.out.println("Current value: " + raf.readInt());
raf.seek(pointer);
System.out.print("New value: ");
raf.writeInt(Keyboard.readInt());
}

// If the offset is not valid, display an error message.

else {
System.out.println("Invalid position");
}

// Ask the user if they want to change another integer value
// and repeat the loop as requested.

System.out.print("Change another value? (Y/N) ");
again = Keyboard.readChar();
} while (again == 'Y' || again == 'y');

// Close the random access file.

raf.close();

// Re-open the file for sequential input and display the
// file's contents.

DataInputStream in = new DataInputStream(new FileInputStream(fd));
try {
int position = 0;
while (true) {
System.out.println(position + ": " + in.readInt());
position++;
}
}
catch (EOFException e) {
System.out.println("");
}
in.close();
System.out.println("Closed - " + fd.getPath());
}

// Catch an IOException if one is thrown.

catch (IOException e) {
System.out.println(e.getMessage());
}
}
}

Limitations and solutions

The usefulness of the RandomAccessFile class is severely restricted by its inability to read and write objects. For example, it would be difficult to use a RandomAccessFile to process a file of Customer objects.

The simplest solution to this problem is to use a Map collection (such as a HashMap) for the objects. All collections are serializable. In a single I/O operation, the entire Map can be written to disk using an ObjectOutputStream or read from disk using an ObjectInputStream. The get() and put() methods can be used to access the collection's objects while the Map is in memory.

Lab exercise for Ferris students

E-mail your answers to this assignment no later than the due date listed in the class schedule.

Review questions

  1. True or False: The seek() method is overloaded to measure the offset of an item of data relative to the end of the file.

  1. True

  2. False

  1. Assume that myFile references a RandomAccessFile containing double values and that an item of data has just been read. Which one of the following must be coded in order to sequentially read the next item of data from the file?

  1. myFile.seek(RandomAccessFile.NEXT);

  2. myFile.seek(myFile.getPointer() + Cool;

  3. myFile.pointer++;

  4. myFile.setPointer(myFile.getPointer() + Cool;

  5. none of the above

  1. Assume that sFile references a RandomAccessFile of short values. Which one of the following correctly positions the pointer in order to read the last short value from the file?

  1. sFile.setPointer(sFile.length() - 2);

  2. sFile.setPointer(sFile.length() - 4);

  3. sFile.seek(sFile.length() - 2);

  4. sFile.seek(sFile.length() - 4);

  5. sFile.seek(RandomAccessFile.LAST);

  1. If theFile is a File object containing a path name, code a single statement to construct a RandomAccessFile object named readOnly that only allows data to be read from the file.


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy