You are on page 1of 24

Types of Java Streams

• 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

• Java byte streams are used to perform input


and output of 8-bit bytes. Though there are
many classes related to byte streams but the
most frequently used classes are,
FileInputStream and FileOutputStream.
Following is an example which makes use of
these two classes to copy an input file into an
output file −
Sample program using FileOutputStream
import java.io.*;
class Simplewrite
{
public static void main(String ar[])
{
try
{
FileOutputStream f = new FileOutputStream ("Sample.txt");
String s = "This the Fileoutputstream program";
byte b[] = s.getBytes(); // convert the string in to byte array
f.write(b);
f.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
Note: to check the output see that a file called abc.txt is created in the path where you
executed this program
Java FileInputStream class obtains input bytes from a file FileOutputStream
import java.io.*;
class Simpleread
{
public static void main(String ar[])
{
try
{
FileInputStream f = new FileInputStream("Sample.txt");
int i = 0;
while((i=f.read())!= -1)
{
System.out.print((char)i);
}
f.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
import java.io.*;
class Simpleread
{
public static void main(String ar[])
{
try
{
FileInputStream f = new
FileInputStream("Sample1.txt");
int i= 0;
i=f.read();
System.out.print((char)i);

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 {

/* Declare and create input file */


FileOutputStream wf = new FileOutputStream("file7.txt");
DataOutputStream wd = new DataOutputStream(wf);

/* Write double in the file */


wd.writeDouble(22.05);

/* Write integer in the file */


wd.writeInt(2014);

/* Write string in the file */


wd.writeUTF("DataStream File");
System.out.println("File Created!");
wd.close();
wf.close();
/* Read the file */
FileInputStream rf = new FileInputStream("file.txt");
DataInputStream rd = new DataInputStream(rf);
System.out.println("Reading File -");

/* Read double from the file */


System.out.println(rd.readDouble());

/* Read integer from the file */


System.out.println(rd.readInt());

/* Read string from the file */


System.out.println(rd.readUTF());
rd.close();
rf.close();
}
}
Java BufferedOutputStream Class

• Java BufferedOutputStream class is used for buffering an output


stream.
• It internally uses buffer to store data. It adds more efficiency
than to write data directly into a stream. So, it makes the
performance fast.
• writing the textual information in the BufferedOutputStream
object which is connected to the FileOutputStream object.
• The flush() flushes the data of one stream and send it into
another.
• It is required if you have connected the one stream with another.
import java.io.*;
public class BufferedOutputStreamExample{
public static void main(String args[])throws Exception{
FileOutputStream fout=new FileOutputStream("buff.txt");
BufferedOutputStream bout=new BufferedOutputStream(fout);
String s="Welcome to java learning.";
byte b[]=s.getBytes();
bout.write(b);
bout.flush();
bout.close();
fout.close();
System.out.println("success");
}
}
import java.io.*;
public class BufferedInputStreamExample{
public static void main(String args[])
{
try
{
FileInputStream fin=new FileInputStream(“buff.txt");
BufferedInputStream bin=new BufferedInputStream(fin);
int i;
while((i=bin.read())!=-1)
{
System.out.print((char)i);
}
bin.close();
fin.close();
}catch(Exception e){System.out.println(e);
}
}
}
Serialization in Java
• Serialization in Java is the process of converting an object
into bytes stream to save it in file.
• The process of recreating object from bytes stream is
called deserialization.
 To serialize an object, its class must implement Serializable interface.

 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.

 To make some data members non serializable, we must declare them as


transient.

 ObjectOutputStream and ObjectInputSteam are two classes that contain


methods to serialize and deserialize an object.

 writeObject() method of ObjectOutputStream class is used to write an object


to file.

 readObject() method of ObjectInputStream class is used to read an object from


file.

 It is a Java convention to give .ser extension to the file in which we are saving
the object.
import java.io.*;

class Employee_new implements Serializable


{
String name;
int salary;
int age;

Employee_new(String name,int salary,int age)


{
this.name=name;
this.salary=salary;
this.age=age;
}
public static void main(String args[]) {
Employee_new e=new Employee_new("neeraj",50000,21);

try {
FileOutputStream fout=new FileOutputStream("data.ser");
ObjectOutputStream out=new ObjectOutputStream(fout);
out.writeObject(e);

FileInputStream fin=new FileInputStream("data.ser");


ObjectInputStream in=new ObjectInputStream(fin);

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 Employee implements Serializable


{
int id;
String name;
static String company="SSS IT Pvt Ltd";//it won't be serialized
public Student(int id, String name) {
this.id = id;
this.name = name;
}
}
A Subclass can be serialized if it implements the Serializable Interface even if a
Superclass does not implement the Serializable Interface.

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.

In this case, the Serialization of the subclass can be prevented by


implementing the writeObject()and readObject() methods in the subclass and
it needs to throw NotSerializableException from these methods.
class Parent implements Serializable {
int i;
public Parent(int i) {
this.i = i;
}
}

class child extends Parent {


int j;
public child(int i, int j) {
super(i);
this.j = j;
}
private void writeObject(ObjectOutputStream out) throws IOException {
throw new NotSerializableException();
}
private void readObject(ObjectInputStream in) throws IOException {
throw new NotSerializableException();
}
}

You might also like