Streams - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Streams

Description:

Sun has created a large number of ways to handle streams of data from files ... catch (IOException exception) {System.out.println (exception) ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 15
Provided by: calpoly
Category:

less

Transcript and Presenter's Notes

Title: Streams


1
Streams
  • Robert Stumpf, Professor
  • Computer Information Systems
  • California State Polytechnic University, Pomona

2
Input/Output
  • Input
  • Output
  • Keyboard input
  • Network input
  • (Recall for security reasons there is no
    network output)

3
Input/Output
  • Sun has created a large number of ways to handle
    streams of data from files
  • Your instructor has chosen only one of ways to
    illustrate this
  • They are
  • BufferedReader and PrintWriter (streams)
  • and
  • FileReader and FileWriter (files)
  • InputStreamReader (keyboard)

4
Input/Output
  • Steams are a sequence of characters being piped
    through a method
  • Files are a model of the physical files on the
    system
  • Both files and streams are needed to make a file
    read work

5
Input
  • Input example
  • BufferedReader in
  • in new BufferedReader (new FileReader
    ("file.txt"))

6
Input
  • To read a record
  • String line
  • line in.readLine ( )
  • To check if end of file
  • line! null

7
Input
  • Best to use loop
  • line in.readLine ( )
  • while (line ! null)
  • System.out.println (line )
  • line in.readLine ( )

8
Output
  • Output example
  • PrintWriter out
  • outputFile new PrintWriter
  • (new FileWriter (file.txt"))

9
Output
  • To write a record with carriage return and line
    feed
  • out.println (line)
  • To write a record with out carriage return and
    line feed
  • out.print (line)

10
Keyboard Input
  • Just substitute System.in for input file name
  • For example
  • BufferedReader keyboard
  • String line
  • keyboard new BufferedReader
  • (new InputStreamReader (System.in))
  • System.out.println ("Enter a line ")
  • line keyboard.readLine ( )

11
Network Input
  • For example
  • BufferedReader inputFile
  • String inputLine
  • try
  • URL url new URL
    (getDocumentBase ( ) , file.txt)
  • in new BufferedReader
    (url.openStream( ) )
  • line in.readLine ( )

12
Network Input
  • Note try statement the catches are
  • catch (MalformedURLException exception)
    System.out.println (exception)
  • catch (IOException exception)
  • System.out.println (exception)
  • catch (SecurityException exception)
  • System.out.println (exception)

13
Summary
  • Practice and test your work
  • Leave the catches in as many problems surface
  • Remember a stream is just characters in motion

14
Thank You
  • Streams are very capable
  • Any questions should be directed to Professor
    Robert Stumpf
  • Email rvstumpf_at_csupomona.edu
  • Web Sitehttp//www.csupomona.edu/rvstumpf
Write a Comment
User Comments (0)
About PowerShow.com