Chapter 14 IO Streams PowerPoint PPT Presentation

presentation player overlay
1 / 18
About This Presentation
Transcript and Presenter's Notes

Title: Chapter 14 IO Streams


1
Session 32
  • Chapter 14 I/O Streams

2
I/O, Java, and Unix - Reminders
  • standard input (stdin) normally means the
    keyboard.
  • standard output (stdout) normally means the
    screen.
  • standard error (stderr) normally means the
    screen.
  • When we use the term echo we simply mean
    repeating something to stdout.

3
A Reminder Exercise
  • Write a quick-and-dirty main program named
    CharacterCounter that displays the number of
    characters on the command line after the command.
    For example
  • gt java CharacterCounter
  • The command line contains 0 character.
  • gt java CharacterCounter a
  • The command line contains 2 characters.
  • gt java CharacterCounter a bc def ghij
  • The command line contains 14 characters.

4
Here is a quick-and-dirty solution
  • public class CharacterCounter
  • public static void main( String args )
  • int characterCount 0
  • for ( int i 0 i lt args.length i )
  • characterCount
  • characterCount argsi.length()
  • System.out.println( "The command line contains
    "
  • characterCount
  • " characters" )
  • 01_CharacterCounter

5
Input Streams in Java
  • We will now consider Javas stream classes. Our
    goal is two-fold
  • Learning their structure will help you improve
    your understanding of inheritance and
    polymorphism.
  • Improving your understanding of inheritance and
    polymorphism will help you to understand the
    Stream classes.
  • (Thats how most of the world works.)

6
Input Streams in Java
  • A stream is a sequence of data. Many modern
    languages define their input/output facilities in
    terms of input streams and output streams.
  • An input stream is a stream that serves as the
    source of data for a program.
  • Java provides a number of different input
    streams, all of which extend the abstract class
    InputStream.
  • http//java.sun.com/javase/6/docs/api/java/io/Inpu
    tStream.html

7
The InputStream Class
  • public abstract class InputStream
  • public abstract int read() throws IOException
  • // Subclasses must implement this method.
  • public int read( byte b ) throws IOException
  • return read( b, 0, b.length)
  • public int read( byte b, int offset, int
    length )
  • // Uses a loop to read a single byte at a time
    into b, using read().
  • // Returns the number of bytes read into the
    buffer, or -1 if at end of the stream
  • public long skip( long n ) throws IOException
  • // The receiver discards n bytes. May, for a
    variety of reasons, skip a smaller
  • // number of bytes, even 0. Returns the actual
    number of bytes skipped.
  • public int available() throws IOException
  • // Subclasses should override this method.
  • return 0
  • public void close() throws IOException

8
The InputStream Class
  • Notice some of the nice features of this class
  • The read method is overloaded. But the more
    complex ones can be implemented in terms of the
    basic method. So we can create subclasses of
    InputStream simply by overriding read().
  • Some methods return two values the read
    modifies an argument object, and the return value
    indicates the status of the operation.

9
Understanding Moreof the InputStream Class
  • We can implement the skip(long n) in terms of
    read().
  • How would you do it?

10
Understanding Moreof the InputStream Class
  • We can implement the skip(long n) in terms of
    read().
  • How would you do it?
  • The basic loop would have to look something like
    this
  • for (int i 0 i lt n i)
  • read()

11
Understanding Moreof the InputStream Class
  • But what happens if the stream contains fewer
    than n characters?
  • The method would throw an exception.
    Fortunately, that is easy to fix
  • public long skip( long n )
  • long numberToRead Math.min(n,available())
  • for (int i 0 i lt numberToRead i)
  • read()
  • return numberToRead

12
Designing Abstract Classes
  • Notice how skip() uses the objects read() and
    available() methods.
  • This sort of abstract design means that few of a
    class methods need to know about the data
    details of the class.
  • Designing classes in this way makes them easier
    to modify and extend.
  • It also means that we can create abstract classes
    that have no data at all yet still implement
    useful behaviors for future users!

13
Physical Input Streams in Java
  • A physical input stream is an input stream that
    reads directly from a real, actual, physical
    source in the machine or in the program.
  • Java provides four physical input stream classes.
    All of these classes override appropriate methods
    inherited from InputStream.
  • In particular, each defines read() to return a
    byte an int that must be cast as a char before
    using it as a character.
  • The four physical input streams are
  • FileInputStream
  • StringBufferInputStream
  • ByteArrayInputStream
  • PipedInputStream

14
An Example of a FileInputStream
  • An instance of this class reads from a file that
    is opened from a file name (a String).
  • import java.io.
  • public class CountBytes
  • public static void main( String args ) throws
    IOException
  • FileInputStream dataSource new
    FileInputStream( args0 )
  • int totalCharacters 0
  • while ( dataSource.read() ! -1 )
  • totalCharacters
  • System.out.println( totalCharacters " bytes"
    )
  • 02_CountBytes

15
An Example of a FileInputStream
  • An instance of this class reads from a file that
    is opened from a file name (a String).
  • import java.io.
  • public class CountBytes
  • public static void main( String args ) throws
    IOException
  • FileInputStream dataSource new
    FileInputStream( args0 )
  • int totalCharacters 0
  • while ( dataSource.read() ! -1 )
  • totalCharacters
  • System.out.println( totalCharacters " bytes"
    )
  • gt java CountBytes CountBytes.java
  • 436 bytes

16
Quick Exercise
  • Modify CountBytes so that it reads from standard
    input whenever the user does not specify a
    filename.

17
A New CountBytes Program
  • public class CountBytes
  • public static void main( String args ) throws
    IOException
  • InputStream dataSource
  • if ( args.length 0 )
  • dataSource System.in
  • else
  • dataSource new FileInputStream( args0 )
  • int totalCharacters 0
  • while ( dataSource.read() ! -1 )
  • totalCharacters
  • System.out.println( totalCharacters " bytes"
    )
  • 03_CountBytes
  • Its that simple, with polymorphic variables and
    substitutable classes.

18
A New CountBytes Program
  • public class CountBytes1
  • public static void main( String args ) throws
    IOException
  • InputStream dataSource
  • if ( args.length 0 )
  • dataSource System.in
  • else
  • dataSource new FileInputStream( args0 )
  • int totalCharacters 0
  • while ( dataSource.read() ! -1 )
  • totalCharacters
  • System.out.println( totalCharacters " bytes"
    )
  • nova gt java CountBytes1
  • erjhgwersljkhgrqegkjlweroig
  • qrelgkjwerao'gqrwpogj'pwoqrg
  • 65 bytes
Write a Comment
User Comments (0)
About PowerShow.com