C SC 160 Chapter 12 topic: File Input/Output
major resource: An Introduction to Object-Oriented Programming with Java, fourth edition, Wu, McGraw Hill, 2006

[ previous | schedule | next ]

Overview

Opening a File

Basic Explanation: Open a file by creating a java.io.File object to represent the open file. Provide the file name as a String argument to the File constructor.

Extended Explanation: The procedure for opening a file is covered in the Basic Explanation. The more interesting part is selecting which file to open.

Using JFileChooser

I/O Streams

File writing and reading operations are based on the concept of streams

Significance of the stream concept: it is device-independent. A method that works with a stream does not know what is at the other end of the stream -- it could be a disk file, an Internet socket, a lab instrument, a remote procedure call...and it doesn't matter!

A stream object is normally connected to a device when the stream is constructed, but the stream methods are the same regardless of the device type.

Stream contents can be stored in either text or binary format When you develop software, you can choose to record file data using either text or binary format. What are the advantages and disadvantages of each?

Writing to a file stream

Java provides a variety of techniques for writing data, each with its own "writer" class. Here are three:

  1. Write raw bytes in binary format
  2. Write primitive types in binary format
  3. Write primitive types in text format (ASCII)
Notice that for the second and third flavors, we started by creating a FileOutputStream object that contained the File, then created a specialized stream object that contained the FileOutputStream object.

FileOutputStream is like a vanilla ice cream cone; you can eat it plain (raw bytes in binary form), or you can choose add-ins like sprinkles (primitives in binary form) or butterscotch dip (primitives in text form) to customize it!

Notice also, that these were all used to write primitive data. What about objects?

Reading from a file stream

File reading operations, like writing, are based on the stream concept.

The basic idea for reading is to use the equivalent technique that was used for writing the data!

Java provides a variety of techniques for reading data, each with its own "reader" class. Here are the three that correspond to the writing techniques described above:

  1. Read raw bytes in binary format
  2. Read primitive types in binary format
  3. Read primitive types in text format (ASCII)
Objects previously saved using writeObject() can be similarly read back in

Closing a stream

When finished with file operations, please close the reader or writer object you were using by invoking its close() method.

Exceptions

Many of the methods above are defined with a throws clause to throw a checked exception (refer to notes on exceptions). A method which invokes such a method must either handle the exception using try - catch or propagate it by adding the same throws clause to its own signature. The latter is normally done, but has to be repeated in the method that invokes it and so forth up the chain all the way to and including main().
[ C SC 160 | Peter Sanderson | Math Sciences server  | Math Sciences home page | Otterbein ]

Last updated:
Peter Sanderson mailto:PSanderson@otterbein.edu