Professional Documents
Culture Documents
• Java byte streams are used to perform input and output of 8-bit bytes.
• Applied for Files
• Java Character streams are used to perform input and output for 16-bit
unicode.
• Applied for characters
Types of Java Streams
Byte stream in java
f.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
Data Streams – A convenient way to read-write primitives in Java
/* Data stream */
import java.io.*;
class datafile {
public static void main(String args[ ]) throws IOException {
Serializable interface does not have any member and so it is called as marker
interface. It tells the Java compiler that the object is serializable.
It is a Java convention to give .ser extension to the file in which we are saving
the object.
import java.io.*;
try {
FileOutputStream fout=new FileOutputStream("data.ser");
ObjectOutputStream out=new ObjectOutputStream(fout);
out.writeObject(e);
e=(Employee_new)in.readObject();
System.out.println(e.name);
System.out.println (e.salary);
System.out.println(e.age);
}
catch(Exception E) {
}
}
}
Java Serialization with Inheritance
If a class implements serializable then all its sub classes will also be serializable.
import java.io.Serializable;
class Person implements Serializable{
int id;
String name;
Person(int id, String name) {
this.id = id;
this.name = name;
class Student extends Person{
}
String course;
}
int fee;
public Student(int id, String name, String course, int fee)
{
super(id,name);
this.course=course;
this.fee=fee;
}
}
If there is any static data member in a class, it will not be serialized because static is
the part of class not object.
class superclass {
int i;
public superclass(int i) {
this.i = i;
}
public superclass() {
i = 50;
System.out.println("Superclass constructor called");
}
}
class subclass extends superclass implements Serializable {
int j;
public subclass(int i, int j) {
super(i);
this.j = j;
}
}
If the superclass is serializable, but we do not need the subclass to
be serialized.