20.2 File Processing and Exception Handling

Specification

  • Write code to perform file-processing operations

    • Open (in read, write, append mode) and close a file

    • Read a record from a file and write a record to a file

    • Perform file-processing operations on serial, sequential, random files

  • Show understanding of an exception and the importance of exception handling

    • Know when it is appropriate to use exception handling

    • Write program code to use exception handling

File processing

There is significant overlap with section 10.3 Files. Make use of both sections for help. In addition, the VB worksheets make use of structures as does the VB companion booklet.

Note: Many online resources refer to sequential access files. Remember though, there is a difference between sequential access and structure. Files are either accessed sequentially or in random access, but the structure of how records are stored within the file is different. Often, the methods shown are serial in nature, as the data is not sorted. A good example is the PDF at the bottom of this page in the files section, which talks about sequential files, but is referring to the access method.

VB.NET and Filestream and Binaryreader / Binarywriter

Random file access in Python is actually slightly simpler than VB,NET, as .NET insists on using modern classes to achieve this, which is much more powerful, but from a student point of view, potentially more confusing. In Python, you can write/read an entire record in one go, in VB, you must write/read each element separately.

The textbook (pp 360 - 365) makes use of a binaryreader and binarywriter to read and write records of text. In the AS, when writing simple text data serially, we used streamreader and streamwriter. However, these can only handle basic text and cannot be used for any access method other than serial or sequential. Remember, the difference between these is that sequential access writes data ordered by a key, where as serial is data written in no order.

You have used StreamWriter (and StreamReader) in the past and the difference is StreamWriter/Reader writes/reads ASCII encoded data, BinaryWriter/Reader uses whatever representation is in memory (e.g. UNICODE, ASCII, etc).

The filestream class deals with writing data as binary, rather than text. Now, you may be thinking that all you are writing is text, and this is correct, but the filestream also gives us the ability to access data in a direct access method, rather than sequentially. The downside, filestream writes and reads bytes only, which means we need to convert the binary back into some encoding method (e.g. reals, strings, dates, etc.). This is why, with a filestream, we also use a BinaryReader or BinaryWriter. This class will take a given encoding stream (e.g. a Boolean value) and write it in a way than can be read back as a Boolean value, etc.

Put another way, the BinaryReader class provides methods that simplify reading primitive data types from a stream. For example, you can use the ReadBoolean method to read the next byte as a Boolean value and advance the current position in the stream by one byte. The class includes read methods that support different data types. This means that you need to specifically read each element of the record using the correct data type method. It will, however, automatically read the entire string without you having to specify how many characters to read.

When you create a new instance of the BinaryReader class, you provide the stream to read from, and optionally specify the type of encoding and whether to leave the stream open after disposing the BinaryReader object. If you do not specify an encoding type, UTF-8 (ASCII equivalent) is used.

This is in contrast to the pseudocode PUTRECORD, which allows you to write an entire record in one go. the SEEK also enables you to go to a record number. Therefore, the VB code can look somewhat different to the pseudocode you are writing.

Online resources

Advanced Topics

Exception Handling