The Artima Developer Community
Sponsored Link

Objects and Java Seminar by Bill Venners
Input and Output
Lecture Handout

Agenda


Input and Output


Output Streams


Input Streams


Using Filters


Example: Buffered Byte Output


Example: Buffered int Output


Example: Buffered int Input


Object Serialization


Why Serialize?


How to Serialize


Serialization Example

1 // In file io/ex4/Chair.java
2 public class Chair
3     implements java.io.Serializable {
4
5     public String toString() {
6         return "Chair";
7     }
8 }

 1 // In file io/ex4/Table.java
 2 public class Table
 3     implements java.io.Serializable {
 4
 5     private Chair[] chairs;
 6
 7     public Table(Chair[] chairs) {
 8
 9         this.chairs = chairs;
10     }
11
12     public String toString() {
13
14         StringBuffer sb = new StringBuffer();
15
16         for (int i = 0; i < chairs.length; ++i) {
17
18             if (i !=0) {
19                 sb.append(", ");
20             }
21             sb.append(chairs[i].toString());
22         }
23
24         return "Table{" + sb.toString() + "}";
25     }
26 }

 1 // In file io/ex4/CoffeeCup.java
 2 public class CoffeeCup
 3     implements java.io.Serializable {
 4
 5     private int innerCoffee;
 6     private Table myTable;
 7
 8     public CoffeeCup(int innerCoffee,
 9         Table myTable) {
10
11         this.innerCoffee = innerCoffee;
12         this.myTable = myTable;
13     }
14
15     public String toString() {
16         return "CoffeeCup{" + innerCoffee
17             + ", " + myTable.toString() + "}";
18     }
19 }

 1 // In file io/ex4/Example4.java
 2 import java.io.*;
 3
 4 public class Example4 {
 5
 6     // Must enter a filename, an amount
 7     // of coffee (in a cup), and a number
 8     // of chairs (at a table) on the command
 9     // line, as in:
10     //
11     // $ java Example4 outfile.dat 50 4
12     //
13     // The above command line will cause this
14     // program to create a CoffeeCup object with
15     // 50 ml of Coffee in it and a Table object
16     // with 4 chairs. The application will then
17     // serialize the CoffeeCup object to the file
18     // named as filename.
19     //
20     public static void main(String[] args)
21         throws IOException {
22
23         if (args.length < 3) {
24
25             System.out.println(
26                 "Must enter filename, coffee, and chairs.");
27             System.exit(0);
28         }
29
30         int amtCoffee = Integer.parseInt(args[1]);
31         int chairCount = Integer.parseInt(args[2]);
32
33         Chair[] chairs = new Chair[chairCount];
34         for (int i = 0; i < chairCount; ++i) {
35             chairs[i] = new Chair();
36         }
37
38         Table table = new Table(chairs);
39         CoffeeCup cup =
40             new CoffeeCup(amtCoffee, table);
41
42         FileOutputStream fos =
43             new FileOutputStream(args[0]);
44
45         BufferedOutputStream bos =
46             new BufferedOutputStream(fos);
47
48         ObjectOutputStream oos =
49             new ObjectOutputStream(bos);
50
51         oos.writeObject(cup);
52         oos.close();
53     }
54 }

$ java Example4 cup.ser 43 3

 1 // In file io/ex5/Example5.java
 2 import java.io.*;
 3
 4 public class Example5 {
 5
 6     // Must enter a filename on the
 7     // command line, as in:
 8     //
 9     // $ java Example5 outfile.dat
10     //
11     // The above command line will cause
12     // this program to attempt to read a
13     // serialized CoffeeCup object from
14     // the file named as filename.
15     //
16     public static void main(String[] args)
17         throws IOException, ClassNotFoundException {
18
19         if (args.length < 1) {
20
21             System.out.println(
22                 "Must enter filename.");
23             System.exit(0);
24         }
25
26         FileInputStream fis =
27             new FileInputStream(args[0]);
28
29         BufferedInputStream bis =
30             new BufferedInputStream(fis);
31
32         ObjectInputStream ois =
33             new ObjectInputStream(bis);
34
35         CoffeeCup cup = (CoffeeCup) ois.readObject();
36         ois.close();
37
38         System.out.println(cup.toString());
39     }
40 }

$ java Example5 cup.ser
CoffeeCup{43, Table{Chair, Chair, Chair}}

Writers


Readers


Character I/O Example

 1 // In file io/ex6/Example6.java
 2 import java.io.*;
 3
 4 public class Example6 {
 5
 6     // Must enter a filename on the
 7     // command line, as in:
 8     //
 9     // $ java Example6 outfile.dat
10     //
11     // The above command line will cause
12     // this program to attempt to read
13     // characters from the named file and
14     // print them to the standard output.
15     //
16     public static void main(String[] args)
17         throws IOException {
18
19         if (args.length < 1) {
20
21             System.out.println(
22                 "Must enter filename.");
23             System.exit(0);
24         }
25
26         // File reader is already buffered.
27         FileReader fr =
28             new FileReader(args[0]);
29
30         OutputStreamWriter osw =
31             new OutputStreamWriter(System.out);
32
33         BufferedWriter bw =
34             new BufferedWriter(osw);
35
36         int c = fr.read();
37         while (c != -1) {
38
39             bw.write(c);
40             c = fr.read();
41         }
42         fr.close();
43         bw.close();
44     }
45 }

Class RandomAccessFile


Class File


Class StreamTokenizer


Exercises

Problem 1.

Write an application named Prettifier.java that reads lines of text from the standard input. For each line read in, prepend the line with the line number followed by a single space, and print this to the standard output.

Problem 2.

In the PackagesAccess/innerclasses/ex3 directory of the sample code, edit Example3.java. At the end of the main() method, write a serialized version of the CoffeeCup object to a file named "freezedried.coffee". To get this to work, you'll have to make changes to several other source files besides Example3.java.

Problem 3.

While still in the PackagesAccess/innerclasses/ex3 directory of the sample code, create a new class named Example3a.java with the usual main() method. Inside this main() method, deserialize the CoffeeCup object which you stored in file freezedried.coffee in Problem 2. Get an iterator from the resurrected CoffeeCup and iterate through the objects contained in the cup. For each object returned by the iterator, print out the String returned by invoking toString() on the object.

Problem 4.

Create an application called Cat that takes zero to many command line arguments. Unlike all other Cat classes used in examples in this course, this Cat actually does something moderately useful. It mimics the functionality of the Unix cat command -- it "concatenates and prints."

If Cat is invoked with no arguments, it reads bytes from the standard input and writes them to the standard output. It continues this process until it reaches end of file on the standard input. If Cat is invoked with one or more arguments, it interprets these command line arguments as filenames. It attempts to open each named file in the order the filenames appear on the command line. It reads each file in turn (byte by byte) and prints it's contents to the standard output. One last request: please buffer your input and output.

Problem 5.

Take the Cat application from Problem 4 and change its name to UniCat. Make UniCat concatenate and print Unicode character files, in the same manner that plain old Cat concatenated and printed bytes. Use Readers and Writers to perform the any necessary character transformations, such as from ASCII in files to Unicode in the program and back to ASCII on the standard output. As usual, buffer your input and output.

Sponsored Links

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us