Chapter 16

Streams

Chapter Goals

Reading Text Files

Writing Text Files

A Sample Program

File LineNumberer.java

Self Check

  1. What happens when you supply the same name for the input and output files to the LineNumberer program?
  2. What happens when you supply the name of a nonexistent input file to the LineNumberer program?

Answers

  1. When the PrintWriter object is created, the output file is emptied. Sadly, that is the same file as the input file. The input file is now empty and the while loop exits immediately.
  2. The program catches a FileNotFoundException, prints an error message, and terminates.

File Dialog Boxes

JFileChooser chooser = new JFileChooser();
FileReader in = null;
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
   File selectedFile = chooser.getSelectedFile();
   reader = new FileReader(selectedFile);
   . . .
}

Text and Binary Formats

Text Format

Binary Format

Reading a Single Character from a File in Text Format

Reading a Single Character from a File in Binary Format

Text and Binary Format

Self Check

  1. Suppose you need to read an image file that contains color values for each pixel in the image. Will you use a Reader or an InputStream?
  2. Why do the read methods of the Reader and InputStream classes return an int and not a char or byte?

Answers

  1. Image data is stored in a binary format–try loading an image file into a text editor, and you won't see much text. Therefore, you should use an InputStream.
  2. They return a special value of -1 to indicate that no more input is available. If the return type had been char or byte, no special value would have been available that is distinguished from a legal data value.

An Encryption Program

To Encrypt Binary Data

int next = in.read();
if (next == -1)
   done = true;
else
{
   byte b = (byte) next;    //call the method to encrypt the byte
   byte c = encrypt(b);
   out.write(c);
}

File Encryptor.java

File EncryptorTester.java

Self Check

  1. Decrypt the following message: Khoor/#Zruog$.
  2. Can you use this program to encrypt a binary file, for example, an image file?

Answers

  1. It is "Hello, World!", encrypted with a key of 3.
  2. Yes–the program uses streams and encrypts each byte.

Public Key Encryption


Random Access vs. Sequential Access

RandomAccessFile

A Sample Program

A Sample Program

A Sample Program

A Sample Program

A Sample Program

File BankDataTester.java

File BankData.java

Output

Account number: 1001
Amount to deposit: 100
adding new account
Done? (Y/N) N
Account number: 1018
Amount to deposit: 200
adding new account
Done? (Y/N) N
Account number: 1001
Amount to deposit: 1000
new balance=1100.0
Done? (Y/N) Y

Self Check

  1. Why doesn't System.out support random access?
  2. What is the advantage of the binary format for storing numbers? What is the disadvantage?

Answers

  1. Suppose you print something, and then you call seek(0), and print again to the same location. It would be difficult to reflect that behavior in the console window.
  2. Advantage: The numbers use a fixed amount of storage space, making it possible to change their values without affecting surrounding data. Disadvantage: You cannot read a binary file with a text editor.

Object Streams

Writing a BankAccount Object to a File

Reading a BankAccount Object from a File

Write and Read an ArrayList to a File

Serializable

File SerialTester.java

Output

First Program Run
   1001:20100.0
   1015:10000.0
Second Program Run
   1001:20200.0
   1015:10000.0

Self Check

  1. Why is it easier to save an object with an ObjectOutputStream than a RandomAccessFile?
  2. What do you have to do to the Coin class so that its objects can be saved in an ObjectOutputStream?

Answers

  1. You can save the entire object with a single writeObject call. With a RandomAccessFile, you have to save each field separately.
  2. Add implements Serializable to the class definition.