java i/o and object serialization

17
JAVA I/O KIRAN V KULKARNI ROLL NO: 28 DIV: A MCA IV SEMESTER NAVNEET PRAKASH ROLL NO: 40 DIV: A MCA IV SEMESTER

Upload: navneet-prakash

Post on 13-Apr-2017

77 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Java I/O and Object Serialization

JAVA I/OKIRAN V KULKARNI

ROLL NO: 28 DIV: AMCA IV SEMESTER

NAVNEET PRAKASHROLL NO: 40 DIV: AMCA IV SEMESTER

Page 2: Java I/O and Object Serialization

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.

Page 3: Java I/O and Object Serialization

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.

Page 4: Java I/O and Object Serialization

BYTE and CHARACTER

Page 5: Java I/O and Object Serialization

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.

Page 6: Java I/O and Object Serialization

System Class Meaning

Page 7: Java I/O and Object Serialization

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.

Page 8: Java I/O and Object Serialization

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.

Page 9: Java I/O and Object Serialization

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-----}

Page 10: Java I/O and Object Serialization

Classes used for Reading and writing objects

The ObjectInputStream class The ObjectOutputStream class

The Classes are foundinjava.io package

Page 11: Java I/O and Object Serialization

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

Page 12: Java I/O and Object Serialization
Page 13: Java I/O and Object Serialization
Page 14: Java I/O and Object Serialization
Page 15: Java I/O and Object Serialization
Page 16: Java I/O and Object Serialization

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}

Page 17: Java I/O and Object Serialization

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.