CSCI A202 - Introduction to Programming (II)

Lecture 20:

  1. File Output, Terminal I/O.
  2. Standard Utilities: Hashtable, Properties

Essential references:

As before, specific links that follow are into Sun's website, unless otherwise specified.

In the first part we cover the use of these kinds of objects:

Page #
1. java.io.FileOutputStream 348
2. java.io.PrintWriter 349
3.
4.
java.io.DataInputStream
java.io.DataOutputStream
356
5.
6.
java.io.ObjectInputStream
java.io.ObjectOutputStream
359
7. java.io.InputStream (System.in) 360
8. java.io.PrintStream (System.out) 360
9.
10.
java.awt.FileDialog
java.io.File
361

Note that FileDialog is part of java.awt package.

Those classes not linked above have a link in Lecture 19 to their API docs.

Here's the worm program discussed in class. It keeps track of how many times it has been called and when. It then saves itself in a file. The blue code is the added level of complexity in the structure of the worm.

Note that the I/O is the same, regardless of the complexity of Worm objects (the blue code adds an instance variable of type Vector to their definition, but the I/O part does not care).

tucotuco.cs.indiana.edu% ls -l Worm*

-rw-r--r-- 1 dgerman students 297 Mar 30 10:28 Worm -rw-r--r-- 1 dgerman students 1640 Mar 30 10:28 Worm.class -rw-r--r-- 1 dgerman students 939 Mar 30 09:08 Worm.java
tucotuco.cs.indiana.edu% cat Worm.java
import java.util.*; import java.io.*; class Worm implements Serializable { int calls; Vector v = new Vector(); public static void main(String[] args) { Worm w; try { FileInputStream fis = new FileInputStream("Worm"); ObjectInputStream ois = new ObjectInputStream(fis); w = (Worm)ois.readObject(); ois.close(); } catch (Exception e) { w = new Worm(); } System.out.println( "I have been called " + w.calls + " times." ); w.calls += 1; w.v.addElement(new Date()); for (int i = 0; i < w.v.size(); i++) System.out.println( "***(" + w.v.elementAt(i).toString() + ")***" ); try { FileOutputStream fos = new FileOutputStream("Worm"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(w); oos.close(); } catch (Exception e) { System.out.println("Something went wrong when writing."); } } }
And here's the worm in action:

tucotuco.cs.indiana.edu% java Worm
I have been called 3 times. ***(Tue Mar 30 09:08:52 EST 1999)*** ***(Tue Mar 30 09:09:01 EST 1999)*** ***(Tue Mar 30 10:28:46 EST 1999)*** ***(Tue Mar 30 13:15:24 EST 1999)***
tucotuco.cs.indiana.edu% date
Tue Mar 30 13:15:26 EST 1999
tucotuco.cs.indiana.edu% java Worm I have been called 4 times. ***(Tue Mar 30 09:08:52 EST 1999)*** ***(Tue Mar 30 09:09:01 EST 1999)*** ***(Tue Mar 30 10:28:46 EST 1999)*** ***(Tue Mar 30 13:15:24 EST 1999)*** ***(Tue Mar 30 13:15:34 EST 1999)***
tucotuco.cs.indiana.edu% date; java Worm; date
Tue Mar 30 13:15:39 EST 1999 I have been called 5 times. ***(Tue Mar 30 09:08:52 EST 1999)*** ***(Tue Mar 30 09:09:01 EST 1999)*** ***(Tue Mar 30 10:28:46 EST 1999)*** ***(Tue Mar 30 13:15:24 EST 1999)*** ***(Tue Mar 30 13:15:34 EST 1999)*** ***(Tue Mar 30 13:15:45 EST 1999)*** Tue Mar 30 13:15:45 EST 1999
tucotuco.cs.indiana.edu%
Note: the example just provided is not intended to provide an example of a self-referential object. Its actual purpose is to point out the significant advantages that object serialization offers.

On Thursday we discuss

kind of objects.

Also, here's the code I didn't get to in class today, that sorts a file in alphabetical order.

tucotuco.cs.indiana.edu% cat Alphabet.java

import java.io.*; import java.util.*; class Alphabet { public static void main(String[] args) { try { StreamTokenizer s = new StreamTokenizer( new BufferedReader( new InputStreamReader( new FileInputStream("input.dat")))); // input.dat is a text file contains the text to be sorted Vector v = new Vector(); s.nextToken(); while (s.ttype != StreamTokenizer.TT_EOF) { if (s.ttype == StreamTokenizer.TT_WORD) v.addElement(s.sval); // Strings are Objects ints are not s.nextToken(); } for (int i = 0; i < v.size() - 1; i++) for (int j = i+1; j < v.size(); j++) if (((String)v.elementAt(i)).compareTo((String)v.elementAt(j)) > 0) { // Vectors store and return Objects String temp = (String)v.elementAt(i); v.setElementAt(v.elementAt(j), i); v.setElementAt(temp, j); } for (int i = 0; i < v.size(); i++) System.out.println(v.elementAt(i)); } catch (Exception e) { System.out.println("E: " + e.toString()); } } }
tucotuco.cs.indiana.edu% cat input.dat
This is the time for all good men.
tucotuco.cs.indiana.edu% java Alphabet
This all for good is men. the time
tucotuco.cs.indiana.edu%
Note that uppercase characters are sorted ahead of their lowercase correspondents.