Random Access Files
permanent and
dynamic structured
data types
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
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.
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();
}
}
Task
Use your text edittor to create three text files. In each file simply write ‘Hello”.
Revision
the size of data types
REFER TO THEORY NOTES ON HEX AND BINARY
A representation of numerical data stored within a binary file
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
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)
Converting Text Files into Binary Files
List all Records with Direct Access
Searching with Direct Access�seek (pos+100) to access each record
one record = 100 bytes
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.
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.