Lecture 2.2.2
Lecture 2.2.2
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science &
Engineering)
PROJECT BASED LEARNING IN JAVA WITH LAB
(22CSH-359/22ITH-359)
TOPIC OF PRESENTATION:
Byte stream, Character stream, Object
serialization, cloning. (CO 4)
DISCOVER . LEARN .
EMPOWER
Lecture Objectives
2
Stream Classes
A stream is an abstraction that either produces or consumes information. A
stream is linked to a physical device by the Java I/O system.
An input stream can abstract many different kinds of input: from a disk file, a
keyboard, or a network socket. Likewise, an output stream may refer to the
console, a disk file, or a network connection.
FileInputStream(String filepath)
FileInputStream(File fileObj) throw a FileNotFoundException
Eg::
FileInputStream f0 = new FileInputStream("/autoexec.bat")
File f = new File("/autoexec.bat");
FileInputStream f1 = new FileInputStream(f);
The mark( ) and reset( ) methods are not overridden, and any attempt to use reset( ) on a
FileInputStream will generate an IOException.
FileOutputStream
FileOutputStream creates an OutputStream that you can use to write bytes to a file.
FileOutputStream(String filePath)
FileOutputStream(File fileObj)
FileOutputStream(String filePath, boolean append)
FileOutputStream(File fileObj, boolean append)
filePath is the full path name of a file, and fileObj is a File object that describes the file.
If append is true, the file is opened in append mode.
ByteArrayInputStream
ByteArrayInputStream is an implementation of an input stream
that uses a byte array as the source.
ByteArrayInputStream(byte array[ ])
ByteArrayInputStream(byte array[ ], int start, int numBytes)
ByteArrayOutputStream
ByteArrayOutputStream is an implementation of an output
stream that uses a byte array as the destination.
BufferedInputStream(InputStream inputStream)
BufferedInputStream(InputStream inputStream, int bufSize)
BufferedOutputStream
BufferedOutputStream(OutputStream outputStream)
BufferedOutputStream(OutputStream outputStream, int bufSize)
The Character Streams
Reader
Reader is an abstract class that defines Java’s model of streaming character input.
It implements the Closeable and Readable interfaces.
Writer
FileReader(String filePath)
FileReader(File fileObj)
throw a FileNotFoundException.
FileWriter
FileWriter creates a Writer that you can use to write to a file.
FileWriter(String filePath)
FileWriter(String filePath, boolean append)
FileWriter(File fileObj)
FileWriter(File fileObj, boolean append)
throw an IOException.
CharArrayReader
CharArrayReader(char array[ ])
CharArrayReader(char array[ ], int start, int numChars)
CharArrayWriter
CharArrayWriter( )
CharArrayWriter(int numChars)
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
• You can only serialize the objects of a class that implements Serializable
interface.
After a serialized object is written to a file, it can be read from t he file and
deserialized (that is we can recreate the object in memory)
package m10.io;
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;
}
}
Object Serialization
// 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
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.
Transient keyword provides us with the ability to control the ser ialization process
and gives us the flexibility to exclude some of object properties from serialization
process.
• Sometimes, it does make sense not to serialize certain attributes of an object. For
e.g. If you are developing an application for Weather forecasting and you have
created objects that store current weather conditions, the n storing current
temperature does not make much sense, since temperature keeps fluctuating and
you may not require the temp data at a later date when you de-serialize this object.
The keyword : transient
import java.io.*;
class Xyz implements Serializable {
double d1;
transient double d2;
static double d3;
void m1() {
System.out.println("The value of d1 is :" +d1);
System.out.println("The value of d2 is :" +d2);
System.out.println("The value of d3 is :" +d3);
}
}
The keyword : transient
class TransientExample1 {
public static void main(String [] args) throws IOException {
Xyz x = new Xyz();
x.d1=10.3;
x.d2=20.5;
x.d3=99.99;
x.m1();
FileOutputStream fx = new FileOutputStream("A1.xyz");
ObjectOutputStream ox = new ObjectOutputStream(fx);
ox.writeObject(x);
ox.flush();
}
}
The keyword : transient
import java.io.*;
class TransientExample2 {
public static void main(String [] args) {
try {
FileInputStream fx = new FileInputStream("A1.xyz");
ObjectInputStream ox = new ObjectInputStream(fx);
Xyz x = (Xyz) ox.readObject();
x.m1();
}
catch(Exception e) {
System.out.println(e);
}
}
}
Summary:
Reference Links:
https://www.javatpoint.com/java-io
https://www.tutorialspoint.com/java/java_files_io.htm
https://docs.oracle.com/javase/tutorial/essential/io/streams.html
https://www.programiz.com/java-programming/io-streams
http://tutorials.jenkov.com/java-io/streams.html
Video Links:
https://youtu.be/3YRahx2ltSg
https://youtu.be/mq-f7zPZ7b8
https://youtu.be/6B6vp0jZnb0
THANK YOU