1 of 13

Random Access Files

permanent and

dynamic structured 

data types

2 of 13

External Data Structures�a binary file to store records in byte format

A binary file is an ordered collection of simple or structured components that are all the same type (type byte). A file location marker keeps track of the next component to be processed. Unlike a text file, you cannot write a binary file using a text editor. The only way to create a binary file is by writing its components using a program. During this course we will be using the .dat extension for all binary files.

Why binary files are needed

3 of 13

Direct-Access Files

RandomAccessFiles API

When files are processed sequentially, each operation is performed on every file component and the components are processed in order, starting with the first. Sequential access is satisfactory whenever a file processing operation affects most of the components in a file. However, for processing only a few components in a large file, accessing every file component would be wasteful and time consuming.

A direct access file is one whose components can be accessed at any time, in any order, by using a seek() method..

For this reason, direct access is also called random access and is analogous to storing a large array on disk.

4 of 13

MacOSX Code

import java.io.File;

import java.io.IOException;

import java.io.RandomAccessFile;

import static java.lang.System.*;

public class AppendRandomAccessFile

{

public static void main(String[] args) throws IOException

{

String fileName = "/Users/julianm/Documents/sdd2018/code/BinaryFile/binFile.dat";

//out.println(fileName);

File file = new File(fileName);

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

raf.seek(raf.length());

raf.writeBytes("SDD\n");

raf.close();

}

}

5 of 13

6 of 13

Task

Use your text edittor to create three text files. In each file simply write ‘Hello”.

  1. UTF8.txt which is saved as plain text UTF 8.
  2. UTF16.txt which is saved as plain text UTF 8.
  3. RTF.rtf which is formatted as rich text
  4. RTFbold.rtf with embold text.

7 of 13

Revision

the size of data types

REFER TO THEORY NOTES ON HEX AND BINARY

A representation of numerical data stored within a binary file

8 of 13

Essential File Structures

fixed length fields

In order to achieve direct access to any record within a binary file we must structure the file according to appropriate field lengths. We can then use the seek() to move the file pointer to the beginning of any record.

30 bytes

50 bytes

20 bytes

String name

String email

String password

restricted length of 28 chars

Restricted length of 48 chars

Restricted length of 18 chars

+ 2 bytes for UTF format

+ 2 bytes for UTF format

+ 2 bytes for UTF format

nameemailpasswordnameemailpasswordnameemailpassword

Seek(0)  read, seek (pos+30)  read, seek(pos+50)  read, seek (pos +20)  read etc

9 of 13

Fixed Length Fields

nameemailpasswordnameemailpasswordnameemailpassword

Seek(0)  read, seek (pos+30)  read, seek(pos+50)  read, seek (pos +20)  read etc

ensuring fields are of fixed length in order to navigate with seek(bytes)

10 of 13

Converting Text Files into Binary Files

11 of 13

List all Records with Direct Access

12 of 13

Searching with Direct Access�seek (pos+100) to access each record

one record = 100 bytes

13 of 13

HSC EXAM VOCAB

Priming Read

What is a priming read? What is its purpose? It is the input operation that takes place just before an input validation loop. The purpose of the priming read is to get the first input value.

    • We have used a priming read when checking if a text file is empty.

Relative File

A relative file contains records ordered by their relative key, a record number that represents the location of the record relative to where the file begins. For example, the first record in a file has a relative record number of 1, the tenth record has a relative record number of 10, and so forth.

    • A relative file must therefore have atleast 1 integer field.
    • The records in a relative file can have either fixed length or variable length. This is because we can seek by searching for the key field and this does not necassarily depend upon record length.
    • We could have a relative sequential text file where the we can jump to the desired record using x number of reads, where x is determined using the relative position of the record.