Java I/O and Object Serialization

Preview:

Citation preview

JAVA I/OKIRAN V KULKARNI

ROLL NO: 28 DIV: AMCA IV SEMESTER

NAVNEET PRAKASHROLL NO: 40 DIV: AMCA IV SEMESTER

WHAT IS A STREAM ? It is an abstraction that either produces or consumes

information.

Stream is linked to a physical device by the Java I/O system.

A stream is a sequence of data.

Stream is composed of bytes.

It's called a stream because it's like a stream of water that continues to flow.

Java.io PACKAGE Java implements streams within class hierarchies

defined in the java.io package.

Input and Output (I/O) is used to process the input and produce the output based on the input.

Java uses the concept of stream to make I/O operations fast.

java.io package contains all the classes required for input and output operations.

BYTE and CHARACTER

THE BYTE STREAM CLASSES

Byte streams are defined by using two class hierarchies.

1. Inputstream

2. Outputstream

Provides convenient means for handling input and output of bytes.

System Class Meaning

THE CHARACTER STREAM CLASSES

Character streams are defined by using two class hierarchies.

1. Reader

2. Writer

Character streams provide a convenient means for handling input and output of characters.

Object Serialization

Serializing objects is the process of writing objects to a file. When an object is serialized, its state and other attributes are

converted into an ordered series of bytes. This byte series can be written into streams.

How to serialize Objects in Java

Any object whose class implements the java.io.Serializable interface can be made persistent

import java.io.*;public class UserData implements Serializable{-----member data-----------member methods-----}

Classes used for Reading and writing objects

The ObjectInputStream class The ObjectOutputStream class

The Classes are foundinjava.io package

ObjectOutputStream

Methods public void writeObject(Object obj)

Write the specified object to the ObjectOutputStream. public void WriteUTF(String str)

Primitive data write of this String in UTF format. public void flush()

Flushes the stream public void close()

Closes the stream

Introduction to Transient variables

You use the transient keyword to indicate to the Java virtual machine that the indicated variable is not part of the persistent state of the object.

Variables that are part of the persistent state of an object must be saved when the object is archived

class MyData implements Serializable{String name;transient int age;---constructor to enterdata}

Summary (Contd.)

• Serialization allows reducing time taken to write code for save and restoration of object or application state

• Serialization makes it easier for objects to travel over a network connection.

• Transient Variables are not persisted.

Recommended