0% found this document useful (0 votes)
5 views21 pages

Institute: Uie Department: Cse: Bachelor of Engineering (Computer Science & Engineering)

The document discusses Object Oriented Programming concepts using Java, focusing on CharArrayReader and CharArrayWriter classes, as well as object serialization and deserialization. It explains how CharArrayReader reads from character arrays and CharArrayWriter writes to them, along with examples of their usage. Additionally, it covers the process of serializing and deserializing objects in Java, including the use of the transient keyword to exclude certain fields from serialization.

Uploaded by

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

Institute: Uie Department: Cse: Bachelor of Engineering (Computer Science & Engineering)

The document discusses Object Oriented Programming concepts using Java, focusing on CharArrayReader and CharArrayWriter classes, as well as object serialization and deserialization. It explains how CharArrayReader reads from character arrays and CharArrayWriter writes to them, along with examples of their usage. Additionally, it covers the process of serializing and deserializing objects in Java, including the use of the transient keyword to exclude certain fields from serialization.

Uploaded by

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

INSTITUTE : UIE

DEPARTMENT : CSE
Bachelor of Engineering (Computer Science &
Engineering)

Object Oriented Programming using Java


(23CSP-202 )
TOPIC OF PRESENTATION:
IOpackage Prepared by:
Bhavneet Kaur
Master Subject
Coordinator

DISCOVER . LEARN . EMPOWER


Lecture Objectives

In this lecture, we will discuss:


• CharArrayReader and
CharArrayWriter
• Object serialization, De-
serialization

2
CharArrayReader
CharArrayReader is an implementation of an input stream
that uses a character array as the source.

CharArrayReader(char array[ ])
CharArrayReader(char array[ ], int start, int numChars)

Class is java.io.CharArrayReader
Read contents of char array[] as acharacter stream
Java CharArrayReader class methods
Example
import java.io.*;
public class CharArrayReaderDemo {
public static void main(String args[]) throws IOException {
String tmp = "abcdefghijklmnopqrstuvwxyz";
int length = tmp.length();
char c[] = new char[length];
tmp.getChars(0, length, c, 0); //public void getChars(int srcBegin, int
srcEnd, // char[] dst, int dstBegin)
CharArrayReader input1 = new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c, 0, 5);
int i;
System.out.println("input1
is:");
while((i = input1.read()) !
= -1) {
System.out.print((char)i);
}
System.out.println();
System.out.println("input2
is:"); input1 object is constructed using the entire
lowercase alphabet,
while((i = input2.read()) !
= -1) { input2 contains only the first five letters.
System.out.print((char)i);
}
CharArrayWriter
CharArrayWriter is an implementation of an output stream that
uses an array as the destination.

CharArrayWriter( ) The characters written to


CharArrayWriter(int numChars) the CharArrayWriter will be
assigned to the elements of the
character array that
the CharArrayWriter is managing.

When the number of characters


written to CharArrayWriter is
greater than the length of the
array, CharArrayWriter will create a
new array with longer length and
copy the characters from the old
array.
Java CharArrayWriter class
methods
Java CharArrayWriter class
methods
Example
import java.io.*;
class CharArrayWriterDemo {
public static void main(String args[]) throws
IOException {

CharArrayWriter f = new CharArrayWriter();


String s = "This should end up in the array";
char buf[] = new char[s.length()];
s.getChars(0, s.length(), buf, 0);
f.write(buf);

System.out.println("Buffer as a string");
System.out.println(f.toString());
System.out.println("Into array");
char c[] = f.toCharArray();
for (int i=0; i<c.length; i++)
{
System.out.print(c[i]);
}
System.out.println("\nTo a
FileWriter()");
FileWriter f2 = new
FileWriter("test.txt");
f.writeTo(f2);
f2.close();
System.out.println("Doing a
reset"); Example demonstrates
CharArrayWriter by reworking
f.reset(); the sample program shown
for (int i=0; i<3; i++) earlier for
f.write('X'); ByteArrayOutputStream.
System.out.println(f.toString());
Object serialization

• Object serialization is the process of saving an


object's state to a sequence of bytes (on
disk), as well as the process of rebuilding
those bytes into a live object at some future
time

• The Java Serialization API provides a standard


mechanism to handle object serialization

• You can only serialize the objects of a class


Object Deserialization
• After a serialized object is
written to a file, it can be
read from the file and
deserialized (that is we can
recreate the object in memory)

• The process of Serialization


and DeSerialization is JVM
independent. That is, an object
can be serialized on one platform
and deserialized on an entirely
different platform.

• Classes ObjectInputStream
How to Write to an ObjectOutputStream
FileOutputStream out = new
FileOutputStream("theTime");
ObjectOutputStream s = new
ObjectOutputStream(out);
s.writeObject("Today");
s.writeObject(new Date());
s.flush();

How to Read from an ObjectOutputStream


FileInputStream in = new
FileInputStream("theTime");
ObjectInputStream s = new
ObjectInputStream(in);
String today = (String) s.readObject();
Example
import java.io.*;
public class MyClass implements
Serializable {
String s;
int i;
double d;
public MyClass(String s, int i, double
d) {
this.s = s;
this.i = i;
this.d = d;
}
public String toString() {
return "s=" + s + "; i=" + i + "; d="
+ d;
public class SerializationDemo {
public static void main(String args[]) {
try {
MyClass object1 = new MyClass("Hello", -7,
2.7e10);
System.out.println("object1; " + object1);
FileOutputStream fos = new
FileOutputStream("serial");
ObjectOutputStream oos = new
ObjectOutputStream(fos);
oos.writeObject(object1);
oos.flush();
oos.close();
}
catch(Exception e) {
System.out.println("Exception during
serialization:"+ e);
// Object Deserialization
try {
MyClass object2;
FileInputStream fis = new
FileInputStream("serial");
ObjectInputStream ois = new
ObjectInputSream(fis);
object2 = (MyClass)ois.readObject();
ois.close();
System.out.println("object2: " + object2);
}
catch(Exception e) {
System.out.println("Exception during
deserialization: " + e);
System.exit(0);
}
}
The keyword : transient
transient keyword is used in Object Serialization.

By default, when you serialize an object, all its fields


are serialized except for static variables. When you
construct this object back from its persistent state,
you will get the values of all the fields that are
serialized(except static variables).

If you do not want to store the value of a particular


non-static field, then you can declare this field as
transient.

This keyword is used only with a variable declaration.


QUIZ:
1. Which of these is a process of writing the state of
an object to a byte stream?
a) Serialization
b) Externalization
c) File Filtering
d) All of the mentioned

2. Which of these is an interface for control over


serialization and deserialization?
a) Serializable
b) Externalization
c) FileFilter
d) ObjectInput
Summary:

In this session, you were able to :

•Learn about CharArrayReader and CharArrayWriter


•Understand the concept of Object serialization, De-
serialization
THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy