CISC 370 Lecture Notes for Class No. 20 April 20, 2000
JAVA I/O Streams

Course Home Prev Lect Next Lect Example Progs Exercise Solns
CoreJava Progs
Documentation
Gen'l Docs Java 2 APIs Java Glossary Term Proj Info

Assignment

Reading:
Horstmann & Cornell, vol I, Chap 12, Streams and Files and/or Reading and Writing (but no 'rithmetic) , the section on input/output from the online Java Tutorial by Campione and Walrath.

Reminder

Term project Milepost 1 is due a week from today, Thursday, May 17.

Today's Lecture Topics

  1. Sign-up for term project presentations if you have not already done so.

  2. Stream Input/Output

    This is the type of I/O that is most similar to that found in other programming languages like C/C++. It can be used to write/read streams of data to/from files, to/from a network, to/from a block of memory. However, the implementation of these facilities in the JDK package java.io.* is likely substantially different from the I/O facilities that you have used in any other language. Some of the issues of which you need to be aware are:

    • Java makes use of the 16-bit Unicode encoding of characters, not the 7- or 8-bit codes like ASCII that have been used in most programming languages to date. Unicode is introduced to bring Java into the modern age in which computing is a world-wide affair that is not primarily restricted to English speaking countries or the scientific community in which English is the common language. With Unicode many of the alphabets used around the world can be represented. However, since Unicode has not been used widely to store data in files around the world, Java must still deal with other encodings like ASCII (see Horstmann and Cornell, vol I, p. 639-41 for a list of local encodings used around the world). Issues that arise with conversions to and from Unicode and the local encodings must be dealt with in Java. These issues are handled by the Reader/Writer classes(and the classes derived from them). These classes work with Unicode character (two bytes) streams.

      There are also classes that are designed to work with ASCII, the predominant encoding. They are InputStream/OutputStream and their derived classes. These classes work with streams in a byte by byte paradigm.

      These two sets of classes provide some common functionality. Here are inheritance diagrams for the two sets of classes. See also, pp. 628-9 of H&C, vol.I.

    • There are restrictions that are normally placed on the I/O that an applet can perform that are not placed on applications. For example, an applet can normally only read/write files that are in the same directory on the same machine from which the applet was down loaded. So all applications that use I/O cannot be turned into applets. For more information about security restrictions on applets and how to appropriately circumvent the restrictions, see the SUN FAQ re applet security .

    • There are facilities for working with files/steams that are in both character and binary form. Character based files can normally be printed and edited with a standard editor. Binary files are in a machine representation that usually cannot be printed or understood by standard editors. Binary files must normally be created by a Java program whereas character files are often created by editors, other programs (including non-Java programs). It is important to know with which type of file you are working.

    • There is a powerful, but simple to use, new facility called serialization in for working with binary files.

  3. I/O Example Programs
    1. A Java program similar to the Unix command cp for copying character files.
    2. A simple, but more specialized, example that demos a use of the File class that enables one to check on attributes of files and directories.

      The two programs described above are in the directory $CISC370HOME/example-progs/io/charIO/ .

    3. Updating a simple data base using character files. This example can be found in the directory $CISC370HOME/example-progs/io/simpleDB/ .

    4. A slightly more complex data base example using binary files. This example can be found in the directory $CISC370HOME/example-progs/io/binaryIO/ .

    5. A similar example to the preceeding that uses object serialization . This example can be found in the directory $CISC370HOME/example-progs/io/serializable/ .

  4. I/O Classes that work together.

    Despite the large number of I/O classes they can be split into eight smaller groups of classes that can be used together. We divide the classes along three dimensions: (1)Input/Output, (2)Character based/Binary based, and (3)ASCII/Unicode coding. This gives us the following eight groups of compatible classes.

    Input Classes (under construction)

    Character Based Binary
    Unicode Reader, BufferedReader  
    ASCII InputStream, BufferedInputStream DataInputStream

    Output Classes (under construction)

    Character Based Binary
    Unicode Writer, BufferedWriter  
    ASCII OutputStream, BufferedOutputStream DataOutputStream

Back to the CISC 370 homepage.

This page has been accessed times since 20 April 2000.

Corrections, suggestions and comments to Bob Caviness .
Last updated 2/21/01.

Copyright 2000 B. F. Caviness