Basic Java I/O

In this discussion, we consider basic Java I/O facilities. A discussion based on Java 1.0 is also available. The relevant classes are contained in the java.io package.

Unlike most of the previous discussion of Java, file I/O will be discussed in the context of a Java application rather than an applet. The reason for this is that browsers, such as Netscape, block access to the local file system as a security precaution. Consequently, you will not be able to execute the example programs discussed below from this tutorial. However, they can be found in the programs directory stored with this tutorial. You may access their source files through your browser, do a view source, and store them within your local file system; after that, you can execute them using the Java Virtual Machine (i.e. java file_name).

An important concept to understand is that in Java files and their data are objects. Thus, the basic strategy you will follow is to first create a File object. It identifies the file in the local file system through a filename and, optionally, a path. When you instantiate a variable of type File, you get an object that you can then pass to various methods. After you create a file object, you will create a data object that you associate with the file object. Thus, you should think of the data in a file as an object whose class provides various methods that allow you to access its contents.

I/O is one of the areas that has changed signifcantly in Java 1.1. In the earlier 1.0 version, I/O for both byte data and character data was handled using classes that derived from InputStream and OutputStream. Frequently used subclasses were FilteredInputStream and FilteredOutputStream.

Java 1.1 still includes the InputStream and OutputStream classes and their subclasses, but they have been assigned primary resposibility for byte I/O. A new set of classes has been added that are intended to be used with character data. The abstract classes that all these character classes derive from are Reader and Writer, with BufferedReader and BufferredWriter likely to be the most frequently used subclasses for basic character I/O. For example, they include convenient methods for reading and writing a line of data and, for input, noting the end_of_file condition.

Java1.1 also includes classes for reading and writing Java objects; however they will not be discussed here.

In the discussion below, we will focus on the File class in order to view and test file attributes and on the BufferedReader and BufferedWriter classes for basic I/O.


Background

Before looking at these I/O classes, we need to consider another Java concept -- Exceptions -- since many of the I/O classes incorporate Java exception handling facilities.

Exceptions

The concept of Exception in basic to Java and is used in a number of contexts in addition to I/O. In an effort to help the programmer write code that is reliable, Java provides a general mechanism that enables the programmer to look for possible run time errors and to take appropriate action when they occur, rather than letting the program continue until possibly crashing or producing erroneous results.


Applications

In this section, we will look at three applications. The first checks attributes for files and directories. The last two illustrate output and input at the "Hello, World!" level. It should be an easy extrapolation from these three example programs to handling more substantial I/O for actual data.

File Attributes

This application illustrates most of the methods included in the File class for accessing the various file attributes, such as length, date of creation, etc.

Output

This application illustrates the use of the BufferedWriter class for writing a stream of character data to a file.

Input

This application illustrates the use of the BufferedReader class for reading a stream of character data from a file.


References

References useful for this discussion include the following: