Java Programming: Working with Data Streams and Serialization, Study notes of Javascript programming

How to use java's datainputstream and dataoutputstream classes to read and write primitive data types to input and output streams. It also covers the objectinputstream and objectoutputstream classes for handling class objects, and the serializable interface for making user-defined classes writable to streams.

Typology: Study notes

Pre 2010

Uploaded on 08/09/2009

koofers-user-cs6
koofers-user-cs6 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming in Java
CSCI 2220
More I/O, Serialization
pf3
pf4
pf5
pf8

Partial preview of the text

Download Java Programming: Working with Data Streams and Serialization and more Study notes Javascript programming in PDF only on Docsity!

Programming in Java

CSCI 2220

More I/O, Serialization

Data Streams

•^

We saw last time how to treat streams as either streamsof bytes or streams of characters

•^

Sometimes we want an even higher level of abstractionand wish to read and write data to and from streams inthe 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 automaticallyhandle converting this information into the necessary rawbytes that a stream can use

DataInputStream DataOutputStream

-^

DataInputStream– readBoolean()– readByte()– readChar()– readDouble()– readFloat()– readInt()– readLong()– readShort()

-^

DataOutputStream has corresponding writemethods

ObjectInputStream ObjectOutputStream

-^

Since usually our programs do not consist of onlyprimitive data types, we also may wish to read and writeclass objects to streams

-^

We can do this with the ObjectInputStream andObjectOutputStream

-^

In fact, the ObjectInputStream and ObjectOutputStreamclasses provide the same functionality asDataInputStream and DataOutputStream, except theyalso include support for reading and writing object datavia readObject() and writeObject() methods

Serialization

•^

A somewhat obvious stipulation of writingobjects to streams is that not only does the classneed to implement Serializable, but all of themembers of the class must also be Serializable

•^

If a member of the class is not Serializable, itcan be declared

transient

and when the

object is written to a stream, that member will bewritten with a value of

null

Homework 5

• That’s all for today• Remember Homework 5 is due in one

week