[go: up one dir, main page]

0% found this document useful (0 votes)
9 views90 pages

Preview File1

The document outlines the course objectives and topics for a programming course focusing on object-oriented design and Java programming. It includes learning outcomes related to understanding and applying object-oriented principles, as well as practical examples of input and output streams in Java. Additionally, it discusses various classes and methods for handling data streams in Java, including byte and character streams.

Uploaded by

mahdeemsh1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views90 pages

Preview File1

The document outlines the course objectives and topics for a programming course focusing on object-oriented design and Java programming. It includes learning outcomes related to understanding and applying object-oriented principles, as well as practical examples of input and output streams in Java. Additionally, it discusses various classes and methods for handling data streams in Java, including byte and character streams.

Uploaded by

mahdeemsh1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

1

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


INDUS UNIVERSITY

2
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY
Course Objectives

To describe programming, algorithm and its structure, sequences, loops and control flow structure.
To illustrate the structured and objective oriented programming, pointer declaration and initialization.
To demonstrate the applications of encapsulation and data hiding, specifies, constructors and inheritance.
To analyze various types of aggregation, composition, overriding, latch and C++
Course Topics / Major Contents

Introduction to object oriented design, history and advantages of object oriented design, introduction to object oriented
programming
concepts, classes, objects, data encapsulation, constructors, destructors, access modifiers, const vs non-const functions, static
data
members & functions, function overloading, operator overloading, identification of classes and their relationships, composition,
aggregation, inheritance, multiple inheritance, polymorphism, abstract classes and interfaces, generic programming concepts,
function & class templates, standard template library, object streams, data and object serialization using object streams,
exception
handling.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Course Learning Outcomes (CLOs)

CLO’s CLO Description PLOs


PLO-1 (C2 -
CLO-1 Understand principles of object oriented paradigm.
Understand)
Identify the objects & their relationships to build object
CLO-2 PLO-3 (C3 - Apply)
oriented solution
Model a solution for a given problem using object oriented
CLO-3 PLO-3 (C3 - Apply)
principles
PLO-2 (C4 -
CLO-4 Examine an object oriented solution
Analyzing)

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


RECOMMENDED BOOKS

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Week 15
Object Stream
Methods of Object Stream
Object Serialization and Deserialization

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Introduction
In Java, streams are the sequence of data that are read from the source and written to the destination.
An input stream is used to read data from the source. And, an output stream is used to write data to the
destination.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Introduction
Class HelloWorld{
Public static void main(String[] args){
System.out.Println("Hello World");
}
}

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Introduction
For example, in our first Hello World example, we have used System.out to print a string.
Here, the System.out is a type of output stream.
Similarly, there are input streams to take input.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Introduction

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Types of Stream
Depending upon the data a stream holds, it can be classified into:
• Byte Stream
• Character Stream

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Byte Stream
Byte stream is used to read and write a single byte (8 bits) of data.
All byte stream classes are derived from base abstract classes
called InputStream and OutputStream.
We will learn it more in:
Java InputStream Class
Java OutputStream Class

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Java InputStream Class
he InputStream class of the java.io package is an abstract superclass that represents an
input stream of bytes.
Since InputStream is an abstract class, it is not useful by itself. However, its subclasses can be
used to read data.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Subclasses of InputStream
In order to use the functionality of InputStream, we can use its subclasses. Some of them are:
• FileInputStream
• ByteArrayInputStream
• ObjectInputStream

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Subclasses of InputStream

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


create an InputStream
In order to create an InputStream, we must import the java.io.InputStream package first.
Once we import the package, here is how we can create the input stream.

//Creates an InputStream
InputStream object1 = new FileInputStream();

Here, we have created an input stream using FileInputStream. It is


because InputStream is an abstract class. Hence we cannot create an object
of InputStream.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Methods of InputStream
The InputStream class provides different methods that are implemented by its subclasses.
Here are some of the commonly used methods:
• read() - reads one byte of data from the input stream
• read(byte[] array) - reads bytes from the stream and stores in the specified array
• available() - returns the number of bytes available in the input stream

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Methods of InputStream
• mark() - marks the position in the input stream up to which data has been read
• reset() - returns the control to the point in the stream where the mark was set
• markSupported() - checks if the mark() and reset() method is supported in the stream
• skips() - skips and discards the specified number of bytes from the input stream
• close() - closes the input stream

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: InputStream Using FileInputStream
•Suppose we have a file named input.txt with the following content:
"This is a line of text inside the file"

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: InputStream Using FileInputStream
• import java.io.FileInputStream;
• import java.io.InputStream;

• class Main {
• public static void main(String args[]) {

• byte[] array = new byte[100];

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: InputStream Using FileInputStream
• try {
• InputStream input = new FileInputStream("input.txt");

• System.out.println("Available bytes in the file: " +
input.available());

• input.read(array);
• System.out.println("Data read from the file: ");

• String data = new String(array);
• System.out.println(data);

• input.close();
• }

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: InputStream Using FileInputStream
• catch (Exception e) {
• e.getStackTrace();
• }
• }
• }

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: InputStream Using FileInputStream
• Output:
• Available bytes in the file: 39
• Data read from the file:
• This is a line of text inside the file

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: InputStream Using FileInputStream
• In the above example, we have created an input stream using the FileInputStream class.
The input stream is linked with the file input.txt.
• InputStream input = new FileInputStream("input.txt");
•To read data from the input.txt file, we have implemented these two methods.
• input.read(array); // to read data from the input stream
• input.close(); // to close the input stream

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Java OutputStream Class
he OutputStream class of the java.io package is an abstract superclass that represents an
output stream of bytes.
Since OutputStream is an abstract class, it is not useful by itself. However, its subclasses can
be used to write data.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Subclasses of OutputStream
In order to use the functionality of OutputStream, we can use its subclasses. Some of them
are:
• FileOutputStream
• ByteArrayOutputStream
• ObjectOutputStream

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Subclasses of OutputStream

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Create an OutputStream
In order to create an OutputStream, we must import the java.io.OutputStream package
first. Once we import the package, here is how we can create the output stream.
// Creates an OutputStream
OutputStream object = new FileOutputStream();
Here, we have created an object of output stream using FileOutputStream. It is
because OutputStream is an abstract class, so we cannot create an object of OutputStream.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Methods of OutputStream
The OutputStream class provides different methods that are implemented by its subclasses.
Here are some of the methods:
• write() - writes the specified byte to the output stream
• write(byte[] array) - writes the bytes from the specified array to the output stream
• flush() - forces to write all data present in output stream to the destination
• close() - closes the output stream

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: OutputStream Using FileOutputStream

import java.io.FileOutputStream;
import java.io.OutputStream;

public class Main {

public static void main(String args[]) {


String data = "This is a line of text inside the file.";

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: OutputStream Using FileOutputStream

try {
OutputStream out = new FileOutputStream("output.txt");
// Converts the string into bytes
byte[] dataBytes = data.getBytes();
// Writes data to the output stream
out.write(dataBytes);
System.out.println("Data is written to the file.");
// Closes the output stream
out.close();
}

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: OutputStream Using FileOutputStream

catch (Exception e) {
e.getStackTrace();
}
}
}

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: OutputStream Using FileOutputStream

In the above example, we have created an output stream using the FileOutputStream class.
The output stream is now linked with the file output.txt.
OutputStream out = new FileOutputStream("output.txt");
To write data to the output.txt file, we have implemented these methods.
output.write(); // To write data to the file
output.close(); // To close the output stream

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: OutputStream Using FileOutputStream

When we run the program, the output.txt file is filled with the following content.
This is a line of text inside the file.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Character Stream
Character stream is used to read and write a single character of data.
All the character stream classes are derived from base abstract classes Reader and Writer.
We will learn it more in:
Java Reader Class
Java Writer Class

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Java Reader Class
The Reader class of the java.io package is an abstract superclass that represents a stream of
characters.
Since Reader is an abstract class, it is not useful by itself. However, its subclasses can be used
to read data.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Subclasses of Reader
In order to use the functionality of Reader, we can use its subclasses. Some of them are:
• BufferedReader
• InputStreamReader
• FileReader
• StringReader

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Subclasses of Reader

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Create a Reader
In order to create a Reader, we must import the java.io.Reader package first. Once we
import the package, here is how we can create the reader.

// Creates a Reader
Reader input = new FileReader();

Here, we have created a reader using the FileReader class. It is because Reader is an
abstract class. Hence we cannot create an object of Reader.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Methods of Reader
The Reader class provides different methods that are implemented by its subclasses. Here are
some of the commonly used methods:
• ready() - checks if the reader is ready to be read
• read(char[] array) - reads the characters from the stream and stores in the specified
array
• read(char[] array, int start, int length) - reads the number of characters
equal to length from the stream and stores in the specified array starting from the start

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Methods of Reader
• mark() - marks the position in the stream up to which data has been read
• reset() - returns the control to the point in the stream where the mark is set
• skip() - discards the specified number of characters from the stream

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: Reader Using FileReader
Here is how we can implement Reader using the FileReader class.
Suppose we have a file named input.txt with the following content.
"This is a line of text inside the file."

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: Reader Using FileReader
import java.io.Reader;
import java.io.FileReader;

class Main {
public static void main(String[] args) {

// Creates an array of character


char[] array = new char[100];

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: Reader Using FileReader

try {
Reader input = new FileReader("input.txt");
System.out.println("Is there data in the stream? " +
input.ready());
input.read(array);
System.out.println("Data in the stream:");
System.out.println(array);
input.close();
}

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: Reader Using FileReader
catch(Exception e) {
e.getStackTrace();
}
}
}

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: Reader Using FileReader
Output:
Is there data in the stream? true
Data in the stream:
This is a line of text inside the file.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: Reader Using FileReader
In the above example, we have created a reader using the FileReader class. The reader is
linked with the file input.txt.
Reader input = new FileReader("input.txt");
To read data from the input.txt file, we have implemented these methods.
input.read(); // to read data from the reader
input.close(); // to close the reader

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Java Writer Class
The Writer class of the java.io package is an abstract superclass that represents a stream of
characters.
Since Writer is an abstract class, it is not useful by itself. However, its subclasses can be used
to write data.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Subclasses of Writer
In order to use the functionality of the Writer, we can use its subclasses. Some of them are:
• BufferedWriter
• OutputStreamWriter
• FileWriter
• StringWriter

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Subclasses of Writer

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Create a Writer
In order to create a Writer, we must import the java.io.Writer package first. Once we
import the package, here is how we can create the writer.
// Creates a Writer
Writer output = new FileWriter();
Here, we have created a writer named output using the FileWriter class. It is because
the Writer is an abstract class. Hence we cannot create an object of Writer

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Methods of Writer
The Writer class provides different methods that are implemented by its subclasses. Here are
some of the methods:
• write(char[] array) - writes the characters from the specified array to the output
stream
• write(String data) - writes the specified string to the writer
• append(char c) - inserts the specified character to the current writer
• flush() - forces to write all the data present in the writer to the corresponding destination
• close() - closes the writer

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: Writer Using FileWriter
import java.io.FileWriter;
import java.io.Writer;

public class Main {

public static void main(String args[]) {

String data = "This is the data in the output file";

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: Writer Using FileWriter
try {
// Creates a Writer using FileWriter
Writer output = new FileWriter("output.txt");

// Writes string to the file


output.write(data);

// Closes the writer


output.close();
}

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: Writer Using FileWriter
catch (Exception e) {
e.getStackTrace();
}
}
}

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example: Writer Using FileWriter
In the above example, we have created a writer using the FileWriter class. The writer is
linked with the file output.txt.
Writer output = new FileWriter("output.txt");
To write data to the output.txt file, we have implemented these methods.
output.write(); // To write data to the file
output.close(); // To close the writer
When we run the program, the output.txt file is filled with the following content.
This is a line of text inside the file.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Java ObjectInputStream Class
The ObjectInputStream class of the java.io package can be used to read objects that were
previously written by ObjectOutputStream.
It extends the InputStream abstract class.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Java ObjectInputStream Class

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Working of ObjectInputStream
The ObjectInputStream is mainly used to read data written by the ObjectOutputStream.
Basically, the ObjectOutputStream converts Java objects into corresponding streams. This is
known as serialization. Those converted streams can be stored in files or transferred through
networks.
Now, if we need to read those objects, we will use the ObjectInputStream that will convert
the streams back to corresponding objects. This is known as deserialization.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Create an ObjectInputStream
In order to create an object input stream, we must import
the java.io.ObjectInputStream package first. Once we import the package, here is how
we can create an input stream.

// Creates a file input stream linked with the specified file


FileInputStream fileStream = new FileInputStream(String file);

// Creates an object input stream using the file input stream


ObjectInputStream objStream = new ObjectInputStream(fileStream)

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Create an ObjectInputStream
In the above example, we have created an object input stream named objStream that is linked
with the file input stream named fileStream.
Now, the objStream can be used to read objects from the file.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Methods of ObjectInputStream
The ObjectInputStream class provides implementations of different methods present in
the InputStream class.
read() Method
• read() - reads a byte of data from the input stream
• readBoolean() - reads data in boolean form
• readChar() - reads data in character form
• readInt() - reads data in integer form
• readObject() - reads the object from the input stream

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example 1: Java ObjectInputStream
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

class Main {
public static void main(String[] args) {

int data1 = 5;
String data2 = "This is programiz";

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example 1: Java ObjectInputStream
try {
FileOutputStream file = new FileOutputStream("file.txt");

ObjectOutputStream output = new ObjectOutputStream(file);


output.writeInt(data1);
output.writeObject(data2);
FileInputStream fileStream = new
FileInputStream("file.txt");

ObjectInputStream objStream = new


ObjectInputStream(fileStream);

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example 1: Java ObjectInputStream
System.out.println("Integer data :" + objStream.readInt());
System.out.println("String data: " +
objStream.readObject());
output.close();
objStream.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example 1: Java ObjectInputStream
Output
Integer data: 5
String data: This is programiz

In the above example, we have used the readInt() and readObject() method to read
integer data and object data from the file.
Here, we have used the ObjectOutputStream to write data to the file. We then read the
data from the file using the ObjectInputStream.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example 2: Java ObjectInputStream
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Dog implements Serializable {

String name;
String breed;

public Dog(String name, String breed) {


this.name = name;
this.breed = breed;
}
} DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY
Example 2: Java ObjectInputStream
class Main {
public static void main(String[] args) {
// Creates an object of Dog class
Dog dog = new Dog("Tyson", "Labrador");
try {
FileOutputStream file = new
FileOutputStream("file.txt");
// Creates an ObjectOutputStream
ObjectOutputStream output = new
ObjectOutputStream(file);
// Writes objects to the output
stream output.writeObject(dog);

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example 2: Java ObjectInputStream
FileInputStream fileStream = new FileInputStream("file.txt");

// Creates an ObjectInputStream
ObjectInputStream input = new ObjectInputStream(fileStream);

// Reads the objects


Dog newDog = (Dog) input.readObject();

System.out.println("Dog Name: " + newDog.name);


System.out.println("Dog Breed: " + newDog.breed);

output.close();
input.close();
}

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example 2: Java ObjectInputStream
catch (Exception e) {
e.getStackTrace();
}
}
}

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example 2: Java ObjectInputStream
Output
Dog Name: Tyson
Dog Breed: Labrador
In the above example, we have created
• ObjectOutputStream named output using the FileOutputStream named file
• ObjectInputStream named input using the FileInputStream named fileStream
• An object dog of the Dog class
Here, we have then used the object output stream to write the object to the file. And, the
object input stream to read the object from the file.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Other Methods Of ObjectInputStream
Methods Descriptions

returns the available number of bytes in the input


available()
stream

marks the position in input stream up to which


mark()
data has been read

returns the control to the point in the input


reset()
stream where the mark was set

skips and discards the specified bytes from the


skipBytes()
input stream

close() closes the object input stream

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Java ObjectOutputStream Class
The ObjectOutputStream class of the java.io package can be used to write objects that
can be read by ObjectInputStream.
It extends the OutputStream abstract class.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Java ObjectOutputStream Class
The ObjectOutputStream class of the java.io package can be used to write objects that
can be read by ObjectInputStream.
It extends the OutputStream abstract class.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Java ObjectOutputStream Class

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Working of ObjectOutputStream
Basically, the ObjectOutputStream encodes Java objects using the class name and object
values. And, hence generates corresponding streams. This process is known as serialization.
Those converted streams can be stored in files and can be transferred among networks.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Create an ObjectOutputStream
In order to create an object output stream, we must import
the java.io.ObjectOutputStream package first. Once we import the package, here is how
we can create an output stream.

// Creates a FileOutputStream where objects from ObjectOutputStream are


written
FileOutputStream fileStream = new FileOutputStream(String file);

// Creates the ObjectOutputStream


ObjectOutputStream objStream = new ObjectOutputStream(fileStream);

In the above example, we have created an object output stream named objStream that is
linked with the file output stream named fileStream.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Methods of ObjectOutputStream

The ObjectOutputStream class provides implementations for different methods present in


the OutputStream class.
write() Method
• write() - writes a byte of data to the output stream
• writeBoolean() - writes data in boolean form
• writeChar() - writes data in character form
• writeInt() - writes data in integer form
• writeObject() - writes object to the output stream

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example 1: Java ObjectOutputStream
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

class Main {
public static void main(String[] args) {

int data1 = 5;
String data2 = "This is programiz";

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example 1: Java ObjectOutputStream
try {

FileOutputStream file = new FileOutputStream("file.txt");

// Creates an ObjectOutputStream
ObjectOutputStream output = new ObjectOutputStream(file);

// writes objects to output stream


output.writeInt(data1);
output.writeObject(data2);

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example 1: Java ObjectOutputStream
// Reads data using the ObjectInputStream
FileInputStream fileStream = new
FileInputStream("file.txt");
ObjectInputStream objStream = new
ObjectInputStream(fileStream);

System.out.println("Integer data :" + objStream.readInt());


System.out.println("String data: " +
objStream.readObject());

output.close();
objStream.close();
}

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example 1: Java ObjectOutputStream
catch (Exception e) {
e.getStackTrace();
}
}
}

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example 1: Java ObjectOutputStream
Output
Integer data: 5
String data: This is programiz

In the above example, we have used the readInt() method and readObject() method to
read an integer data and object data from the files.
Here, we have used the ObjectOutputStream to write data to the file. We then read the
data from the file using the ObjectInputStream.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example 2: Java ObjectOutputStream
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Dog implements Serializable {

String name;
String breed;

public Dog(String name, String breed) {


this.name = name;
this.breed = breed;
}
} DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY
Example 2: Java ObjectOutputStream
class Main {
public static void main(String[] args) {

// Creates an object of Dog class


Dog dog1 = new Dog("Tyson", "Labrador");

try { FileOutputStream fileOut = new


FileOutputStream("file.txt");

// Creates an ObjectOutputStream
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
// Writes objects to the output stream
objOut.writeObject(dog1);
// Reads the object

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Example 2: Java ObjectOutputStream
// Reads the objects
Dog newDog = (Dog) objIn.readObject();

System.out.println("Dog Name: " + newDog.name);


System.out.println("Dog Breed: " + newDog.breed);

objOut.close();
objIn.close();
}

catch (Exception e) {
e.getStackTrace();
}
}
}
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY
Example 2: Java ObjectOutputStream
Output
Dog Name: Tyson
Dog Breed: Labrador

In the above example, we have created


• ObjectOutputStream named objOut using the FileOutputStream named fileOut
• ObjectInputStream named objIn using the FileInputStream named fileIn.
• An object dog1 of the Dog class.
Here, we have then used the object output stream to write the object to the file. And, the
object input stream to read the object from the file.

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Other Methods Of ObjectOutputStream

Methods Descriptions

flush() clears all the data from the output stream

drain() puts all the buffered data in the output stream

close() closes the output stream

DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY


Thank Reference Books
C++ How To Program 10th Edition Deitel & Deitel
Objet oriented Programming in C++ 3rd Edition By Robert Lafore

You
.
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 86
Thank
You
.
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 86

You might also like