Programming in Java CSCI 2220 - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Programming in Java CSCI 2220

Description:

We saw last time how to treat streams as either streams of bytes or streams of characters ... Save program state information to a file ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 9
Provided by: owenke
Category:

less

Transcript and Presenter's Notes

Title: Programming in Java CSCI 2220


1
Programming in JavaCSCI 2220
  • More I/O, Serialization

2
Data Streams
  • We saw last time how to treat streams as either
    streams of bytes or streams of characters
  • Sometimes we want an even higher level of
    abstraction and wish to read and write data to
    and from streams in the form of primitive data
    variables or entire class objects
  • Save program state information to a file
  • Send data variables to different threads,
    processes, or across network streams
  • Many other examples
  • Java has built in stream classes that
    automatically handle converting this information
    into the necessary raw bytes that a stream can use

3
DataInputStreamDataOutputStream
  • The DataInputStream and DataOutputStream classes
    respectively allow you to read and write
    primitive data types to input and output streams

BufferedOutputStream bufStream DataOutputStream
dataStream try bufStream new
BufferedOutputStream(new FileOutputStream(file.ou
t) dataStream new DataOutputStream(bufStrea
m) dataStream.writeInt(5)
catch(IOException e) System.err.println(Erro
r writing to file) finally try
dataStream.close() bufStream.close()
catch(IOException e) System.err.println(E
rror closing file)
4
DataInputStreamDataOutputStream
  • DataInputStream
  • readBoolean()
  • readByte()
  • readChar()
  • readDouble()
  • readFloat()
  • readInt()
  • readLong()
  • readShort()
  • DataOutputStream has corresponding write methods

5
ObjectInputStreamObjectOutputStream
  • Since usually our programs do not consist of only
    primitive data types, we also may wish to read
    and write class objects to streams
  • We can do this with the ObjectInputStream and
    ObjectOutputStream
  • In fact, the ObjectInputStream and
    ObjectOutputStream classes provide the same
    functionality as DataInputStream and
    DataOutputStream, except they also include
    support for reading and writing object data via
    readObject() and writeObject() methods

6
Serialization
  • Objects can only be written to a stream if they
    are of a class type that implements the
    Serializable interface
  • Most built in classes in Java implement
    Serializable
  • Serializable does not define any methods. So in
    order to make a user defined class capable of
    being written to a stream, usually you can just
    append implements Serializable to the class
    declaration and it will work

7
Serialization
  • A somewhat obvious stipulation of writing objects
    to streams is that not only does the class need
    to implement Serializable, but all of the members
    of the class must also be Serializable
  • If a member of the class is not Serializable, it
    can be declared transient and when the object is
    written to a stream, that member will be written
    with a value of null

8
Homework 5
  • Thats all for today
  • Remember Homework 5 is due in one week
Write a Comment
User Comments (0)
About PowerShow.com