Chapter 14 IO Streams - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Chapter 14 IO Streams

Description:

StringBufferInputStream ( deprecated ) ByteArrayInputStream. PipedInputStream ... First, it has been deprecated so we get a warning, but we will still use it... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 16
Provided by: BenSc
Category:

less

Transcript and Presenter's Notes

Title: Chapter 14 IO Streams


1
Session 33
  • Chapter 14 I/O Streams

2
PA06 Handed Back
  • You need only a limited number of IVs in
    DomeWorld
  • sliders and a score
  • You should NOT have a Football/projectile
  • You should NOT have distance/height vars
  • Notice that a reasonable amount of
    drawProjectile() is duplicate code. This
    suggests you can refactor the original version in
    ProjectileWorld
  • Refactor so it uses an onTarget() method then
    only override that method in DomeWorld.

3
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

4
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

5
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 stream classes are
  • FileInputStream
  • StringBufferInputStream ( deprecated )
  • ByteArrayInputStream
  • PipedInputStream

6
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.

7
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"
    )
  • By the way, notice that I am cheating a little
    bit in order to make this code more readable in
    notes.
  • We should normally use try/catch blocks instead.

8
An Example of aStringBufferInputStream
  • What do you think happens if I change the
    InputStream???
  • import java.io.
  • public class CountBytes
  • public static void main( String args ) throws
    IOException
  • InputStream dataSource
  • if ( args.length 0 )
  • dataSource System.in
  • else
  • dataSource new StringBufferInputStream(
    args0 )
  • int totalCharacters 0
  • while ( dataSource.read() ! -1 )
  • totalCharacters
  • System.out.println( totalCharacters " bytes"
    )
  • 04_CountBytes

9
An Example of aStringBufferInputStream
  • Two things
  • First, it has been deprecated so we get a
    warning, but we will still use it
  • Second, an instance of this class reads from a
    String object! Since the String is treated as a
    String rather than a file name, you simply get
    the length of the String.
  • gt java CountBytes CountBytes.java
  • 15 bytes

10
A StringBufferInputStream Exercise
  • Modify the following program so that it counts
    the characters in all of the command-line
    arguments. (Do not count the spaces between the
    arguments, as we did in CharacterCounter before.)
  • public class CountBytes
  • public static void main( String args ) throws
    IOException
  • InputStream dataSource
  • if ( args.length 0 )
  • dataSource System.in
  • else
  • dataSource new StringBufferInputStream(
    args0 )
  • int totalCharacters 0
  • while ( dataSource.read() ! -1 )
  • totalCharacters
  • System.out.println( totalCharacters " bytes"
    )

11
A Possible Solution
  • public class CountBytes
  • public static void main( String args ) throws
    IOException
  • InputStream dataSource
  • int totalCharacters 0
  • for ( int i 0 i lt args.length i )
  • dataSource new StringBufferInputStream(
    argsi )
  • while ( dataSource.read() ! -1 )
  • totalCharacters
  • System.out.println( totalCharacters " bytes"
    )
  • 05_CountBytes

12
A Possible Solution
  • public class CountBytes
  • public static void main( String args ) throws
    IOException
  • InputStream dataSource
  • int totalCharacters 0
  • for ( int i 0 i lt args.length i )
  • dataSource new StringBufferInputStream(
    argsi )
  • while ( dataSource.read() ! -1 )
  • totalCharacters
  • System.out.println( totalCharacters " bytes"
    )
  • gt java CountBytes CountBytes.java
  • 15 bytes
  • gt java CountBytes not that theres anything wrong
    with that
  • 34 bytes
  • How can we best handle standard input now?

13
Modifying Our Solution, Part 1
  • public class CountBytes
  • public static void main( String args ) throws
    IOException
  • InputStream dataSource
  • int totalCharacters 0
  • if ( args.length ! 0 )
  • for ( int i 0 i lt args.length i )
  • dataSource new StringBufferInputStream(args
    i)
  • while ( dataSource.read() ! -1 )
  • totalCharacters
  • else
  • dataSource System.in
  • while ( dataSource.read() ! -1 )
  • totalCharacters
  • System.out.println( totalCharacters " bytes"
    )
  • But what is wrong with this???

14
Modifying Our Solution, Part 2
  • public class CountBytes
  • public static void main( String args ) throws
    IOException
  • InputStream dataSource
  • int totalCharacters 0
  • if ( args.length ! 0 )
  • for ( int i 0 i lt args.length i )
  • totalCharacters computeCharactersIn(
  • new StringBufferInputStream( argsi ) )
  • else
  • totalCharacters computeCharactersIn(
    System.in )
  • System.out.println( totalCharacters " bytes"
    )
  • protected static int computeCharactersIn(
    InputStream source )
  • throws IOException
  • int totalCharacters 0
  • while ( source.read() ! -1 )
  • totalCharacters
  • return totalCharacters

15
Other Physical Input Streams
  • ByteArrayInputStream
  • A ByteArrayInputStream is like a
    StringBufferInputStream, but the chars are stored
    in (you guessed it) a byte object.
  • PipedInputStream
  • An instance of this class reads from a
    PipedOutputStream object as if it were a file.
    This allows one part of a program to generate a
    stream of data and another part to read the data
    as if the stream already existed. (Think of
    piping in Unix or DOS for an application...)
  • The sequence of characters offered by a
    PipedInputStream comes from the output of another
    program, which writes to PipedOutputStream.
  • Why have these?
  • To shield the client code from data
    representation.
Write a Comment
User Comments (0)
About PowerShow.com