Docsity

Random Access Files - Java and the Web - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Random Access Files - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Random Access Files

Random Access Files

  • Random access files are files in which records can be accessed in any order - Also called direct access files - More efficient than sequential access files

Example

  • Consider the zip format. A ZIP archive contains files and is typically compressed to save space. It also contain a directory entry at the end that indicates where the various files contained within the ZIP archive begin

Accessing a specific file using

sequential access

  • Open the ZIP archive.
  • Search through the ZIP archive until you locate the file you want to extract.
  • Extract the file.
  • Close the ZIP archive.

On an average, we have to read half of the zip archive to find the required file

RandomAccessFiles class

  • The RandomAccessFile class contains the same read(), write() and close() methods as Input and OutputStream
  • Also contains seek() that lets you select a beginning position within the file before reading or writing data
  • Includes capabilities for reading and writing primitive-type values, byte arrays and strings

RandomAccessFile Class

  • NOT compatible with the stream/reader/writer models
  • With a random-access file, you can seek to the desired position and then read and write an amount of bytes
  • Only support seeking relative to the beginning of the file
    • Not relative to current position of file pointer
    • However there are methods that report the current position

Constructor Summary

RandomAccessFile(File file, String mode) Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument. RandomAccessFile(String name, String mode) Creates a random access file stream to read from, and optionally to write to, a file with the specified name.

  • The mode should be either “r” or “rw”
    • No “w”

Constructor Summary

• When a RandomAccessFile is created in read-

only mode a FileNotFoundException is

generated

• When a RandomAccessFile is created in read-

write a zero length file will be created

Manipulate file pointer

  • RandomAccessFile contains three methods for explicitly manipulating the file pointer.
  • int skipBytes(int) — Moves the file pointer forward the specified number of bytes
  • void seek(long) — Positions the file pointer just before the specified byte
  • long getFilePointer() — Returns the current byte location of the file pointer
  • To move the file pointer to a specific byte

f.seek(n);

  • To get current position of the file pointer.

long n = f.getFilePointer();

  • To find the number of bytes in a file

long filelength = f.length();

// set pointer to the beginning of file and read next two items myfile.seek(0); System.out.println (myfile.readInt()); System.out.println

(myfile.readDouble()); //set pointer to the 4th item and read it myfile.seek(16); System.out.println

(myfile.readBoolean()); // Go to the end and “append”

an integer 2003 myfile.seek(myfile.length()); myfile.writeInt(2003); // read 5th and 6th items myfile.seek(17); System.out.printlna

Int

Double

Int boolean Char Int

0

4

12

16 17 19

23

Output

Reading Example

  • RandomAccessFile creates random access files
    • Contains familiar read( ), write( ), open( ), close( )
    • seek( ) method selects start position within a file

before read or write

  • Places a file pointer at the selected location
  • File pointer holds byte number of next file

position

  • Ex: locate 32 nd^ character in file, which may then

be read