CS 162 Spring 2004 - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

CS 162 Spring 2004

Description:

... (as in C, C ) is fine for Roman alphabets, not for rest of world (Chinese, Thai, etc) ... Write to an ObjectOutputStream or read from an objectInputStream ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 23
Provided by: timoth90
Category:

less

Transcript and Presenter's Notes

Title: CS 162 Spring 2004


1
CS 162 - Spring 2004
  • Chapter 15
  • Streams

2
Streams versus Files
  • Streams are an abstraction that generalizes the
    idea of files
  • A stream is simply a sequence of characters
  • Could be from a file,
  • A network connection
  • A buffer,
  • Or someplace else

3
Binary versus Text Format
  • Files are traditionally thought of as in one of
    two different format
  • Binary format. Just a string of uninterpreted
    bits (or bytes, which are generally more
    efficient to access)
  • Text files. Storage is same, but the
    interpretation of bits as is text

4
Storing 12,345
  • The value 12,345 stored as binary would be
  • 00 00 48 57 (48 256 57 12,345)
  • Stored as text would be
  • 49 50 51 52 53
  • Which is the ASCII representation of each
    character

5
Unicode
  • Java has generalized the traditional view of the
    char data type
  • An 8-bit char (as in C, C) is fine for Roman
    alphabets, not for rest of world (Chinese, Thai,
    etc)
  • The 16-bit Unicode character is a newer more
    international standard

6
Files Streams and Readers
  • Java I/O is actually composed of two related
    libraries
  • The Stream library is used to read 8-bit
    character values. Used for binary I/O
  • The Reader/Writer library is used to work with 16
    bit character values. Used for text I/O. Layered
    on top of the Stream library.

7
Reading from a stream
  • FileInputStream inputStream new
    FileInputStream("input.dat")
  • Int next inputStream.read()
  • byte b
  • If (next ! -1)
  • b (byte) next

8
Reading from Reader
  • FileReader reader new FileReader("input.dat")
  • Int next reader.read()
  • Char c
  • If (next ! -1)
  • c (char) next

9
Writing/Closing a file
  • FileWriter writer new FileWriter("out.put")
  • Char c '!'
  • Writer.write( c )
  • Writer.close() // important, makes changes

10
Wrappers
  • File streams and reader/writers provide only the
    basic facility for reading and writing a single
    character.
  • Other useful functionality is provided by
    wrappers - classes that wrap around basic files
    and provide new methods

11
Printing
  • An example wrapper, PrintWriters
  • PrinterWriters provide a number of useful
    multi-character printing routines, I.e. print and
    println, also conversion from primitives
  • FileWriter writer new FileWriter("out.put")
  • PrintWriter out new PrintWriter(writer)
  • Out.println("Hello world") out.println(42)

12
Another wrapper, BufferedReader
  • Another useful wrapper, BufferedReader provides
    the method readLine
  • FileReader fread new FileeReader("in.txt")
  • BufferedReader in new BufferedReadeer(fred)
  • String inputLine in.readLine()

13
Readers are actually Wrappers
  • In fact, readers/writers are actually wrappers
    around files. FileReader constructor is short for
  • File f new File("raw.data")
  • FileReader fr new FileReader(f)

14
Input Stream Hierarchy
  • InputStream
  • ByteArrayInputStream
  • FileInputStream
  • FilterInputStream
  • DataInputStream (e.g., System.in)
  • BufferedInputStream (reads an array more
    efficiently)
  • LineNumberInputStream (keeps track of line
    numbers)
  • PushbackInputStream (can unread characters)
  • PipedInputStream (can connect to piped output
    stream)
  • ObjectInputStream
  • SequenceInputStream

15
Output Stream Hierarchy
  • OutputStream
  • ByteArrayOutputStream
  • FileOutputStream
  • FilterOutputStream
  • DataOutputStream
  • BufferedOutputStream
  • PrintStream (e.g. System.out)
  • PipedOutputStream
  • ObjectOutputStream

16
Reader Hierarchy (there's a similar one for
Writer)
  • Reader
  • BufferedReader
  • LineNumberReader
  • CharArrayReader
  • InputStreamReader
  • FileReader
  • FilterReader
  • PushbackReader
  • PipedReader
  • StringReader

17
File Dialogs
  • If you have a graphical application there is an
    easy way to create a file dialog
  • JFilChooser chooser new JFileChooser()
  • FileReader in null
  • If (chooser.showOpenDialog(null)
    JFileChooser.APPROVE_OPTION)
  • File selectedFile chooser.getSelectedFile()
  • in new FileReader(selectedFile)

18
Object Streams
  • Complex objects are often difficult to store
  • They have internal state
  • Two names can reference the same value
  • They mix primitive/object values
  • Writing your own code to store/read such values
    can be tedious and error prone.

19
Writing Objects
  • Java makes storing even complex objects almost
    trivial
  • Declare objects as Serializable
  • Write to an ObjectOutputStream or read from an
    objectInputStream
  • Notice this is stream (binary I/O) not text files

20
Writing to a Stream
  • Coin c
  • ObjectOutputStream out new ObjectOutputStream(ne
    w FileOuputStream("coins.dat")
  • Out.writeObject( c )
  • It's that simple.

21
Reading an Object
  • ObjectInputStream in new ObjectInputStream(new
    FileInputStream("in.put"))
  • Coin c (Coin) in.readObject()
  • Its's that simple. Note Can throw cast exception.

22
Questions?
  • Using Files and Readers in Assignment 3.
  • Read Chapter 16 and Chapter 3 from my book Intro
    to OOP (from course webpage).
Write a Comment
User Comments (0)
About PowerShow.com