File Input and Output

There are two types of files in Java - text files and binary files. Files provide both sequential and random access. A text file is processed as a sequence of characters. A binary file is processed as a sequence of bytes. In a text file you have the illusion that the file is divided into lines. There is a special end-of-line symbol that creates this illusion. In addition you can think that there is a special end-of-file symbol that follows the last component in a file. A big advantage of text files is their portability. In binary files, the representation used varies from computer to computer. Java binary files are platform independent. They can be interpreted by any computer that supports Java.

A stream is a device for transmitting or retrieving 8-bit or byte values. The emphasis is on the action of reading or writing as opposed to the data itself. A file is a collection of items stored on an external device. The Java object FileStream provides the means to access the data values but does not actually hold the file contents.

There are two independent and largely parallel systems involved in I/O. InputStream and OutputStream are used to read and write 8-bit quantities and process binary files. The alternative hierarchy has two different classes Reader and Writer that are used to read and write 16-bit Unicode character values and process text files.

There are classes tied to a physical input source. These read values from a byte array, a file, or a pipe (e.g. a FileInputStream requires either a file or a file name). There are also virtual input classes that rely on one or more underlying input streams for their data source. For example the class PushbackInputStream adds the ability to unread already processed characters, returning them once more in response to a subsequent read operation.

One type of filtering is buffering which is provided by several buffered streams including BufferedInputStream and BufferedReader for performing binary and text input. BufferedOutputStream and BufferedWriter for binary and text outputs. A buffer is a relatively large region of memory used to temporarily store data while it is being input or output.

StreamTokenizer breaks a textual file into a sequence of tokens. It recognizes words and numbers.

The File class provides methods for dealing with files or directories. File systems are organized into a hierarchy. A path is a description of a file's location in the hierarchy. When a program is running, the program' directory is considered the current directory. Any files located in the current directory can be referred to by name alone. The relative path is the location of a file with respect to the current directory. The absolute path starts at the root directory.

public class File extends Object implements Serializable
{
  // Constructors
  public File ( String path );
  public File ( String path, String name );

  // Public methods
  public boolean canRead();	// is the file readable?
  public boolean canWrite();	// is the file writeable?
  public boolean delete();	// delete the file
  public boolean exists();	// does the file exist
  public long lastModified();	// when was the file last modified
  public long length();		// How many bytes does it contain
  public boolean renameTo(File f);   // rename this file to f's name
}

Creating a Handle to a File

A handle to a file is created by passing the name of the file to the constructor for the File object:
File inFile = new File ( "FileIO.txt" );

Reading from a Text File

Reading from a text file is facilitated by the class FileReader. Unfortunately, the methods available from the class FileReader are not convenient for text processing. We usually embed this stream within a BufferedReader object. FileReader will throw the exception FileNotFoundException so the input statements should be enclosed in a try block and the exception handled in the catch block.

import java.io.*;

public class ReadTextFile
{
  public static void main (String [] args) throws IOException
  {
    File inFile = new File ("input.txt");
    FileReader fReader = new FileReader (inFile);
    BufferedReader bReader = new BufferedReader (fReader);

    try
    {
      String line = bReader.readLine();
      while (line != null)
      {
        System.out.println (line);
        line = bReader.readLine();
      }
    }
    catch (Exception e)
    {
      System.out.println (e.getMessage());
      e.printStackTrace();
      System.exit(0);
    }
    finally
    {
      fReader.close();
    }
  }
}

A text file can also be read using a Scanner object. Using the Scanner offers the advantage of using the methods that come with the Scanner class.

import java.util.Scanner;
import java.io.File;

public class ReadTextFile
{
  public static void main (String [] args) throws IOException 
  {
    File inFile = new File ("input.txt");

    Scanner sc = new Scanner (inFile);
    while (sc.hasNextLine())
    {
      String line = sc.nextLine();
      System.out.println (line);
    }
    sc.close();
  }
}

Writing to a Text File

To write text to a file you open an output stream by using the class FileWriter. If the file does not exist a new empty file with this name is created. If the file already exists opening it erases the data in the file. If you want to append to the file use the following option when creating the FileWriter object:
FileWriter fWriter = new FileWriter (outFile, true);
The class PrintWriter has methods print(), printf() and println() that will allow us to write to a file.

import java.io.*;

public class WriteTextFile
{
  public static void main (String [] args) throws IOException
  {
    File outFile = new File ("output.txt");
    FileWriter fWriter = new FileWriter (outFile);
    PrintWriter pWriter = new PrintWriter (fWriter);
    pWriter.println ("This is a line.");
    pWriter.println ("This is another line."):
    pWriter.close();
  }
}

Binary Files

When designing binary file I/O applications it is a good idea to design the input and output methods together. It is convenient to save data of different types such as int, double, and String to files in such a way that these can be read back as integers, doubles, and strings.

Writing to a Binary File

The class DataOutputStream is used to output several types of data. It has methods: UTF stands for Unicode Text Format. It is the coding scheme for Java's Unicode character set.

import java.io.*;

public class WriteBinaryFile
{
  public static void main (String [] args) throws IOException
  {
    File outFile = new File ("output.bin");
    FileOutputStream outStream = new FileOutputStream (outFile);
    DataOutputStream output = new DataOutputStream (outStream);

    String name = "John Doe";
    long ssNum = 123456789;
    double gpa = 3.85;

    try
    {
      output.writeUTF (name);
      output.writeLong (ssNum);
      output.writeDouble (gpa);
    }
    catch (Exception e)
    {
      System.out.println (e.getMessage());
      e.printStackTrace();
      System.exit(0);
    }
    finally
    {
      outStream.close();
    }

  }
}

Reading from a Binary File

The class DataInputStream is used to input several types of data. This class has the following methods that allows us to input data:
import java.io.*;

public class ReadBinaryFile
{
  public static void main (String [] args) throws IOException
  {
    File inFile = new File ("input.bin");
    FileInputStream inStream = new FileInputStream (inFile);
    DataInputStream input = new DataInputStream (inStream);

    String name;
    long ssNum;
    double gpa;

    try
    {
      while (true)
      {
        name = input.readUTF();
        ssNum = input.readLong();
        gpa = input.readDouble();

        System.out.println (name + "  " + ssNum + "  " + gpa);
      }
    }
    catch (EOFException e)
    {
      // Do nothing if it is the end of file.
    }
    catch (Exception e)
    {
      System.out.println (e.getMessage());
      e.printStackTrace();
      System.exit(0);
    }
    finally
    {
      inStream.close();
    }

  }
}