Java Streams & Serialization

Java Streams:
A stream is a flow of data from source to destination.
A source can be a keyboard, file, client, server, ... etc.,
A destination can be a monitor, file, client, server, .. etc.,

In Java, streams are divided into 3 categories:

1) Console Input/Output Streams
2) File Input/Output Streams
3) Network Input/Output Streams

Predefined Streams:

There are 3 predefined streams
1) in
2) out
3) err

"in" is an object reference of java.io. InputStream class

"out" & "err" are object reference of java.io.PrintStream 
class.

The above all predefined streams are static members of

java.lang.System class.

Differences between System.out & System.err


System.out                              System.err

1) It is used to display           1) It is used to display 
     output messages.                   error messages.
2) This stream data can         2) This stream data cannot 
     be redirected to a file.            be redirected to a file.

Example:

class Demo
{   
          public static void main(String args[])
          {
                    System.out.println("Core Java");
                    System.err.println("Advanced Java");
          }
}

C:\> javac Demo.java

C:\> java Demo
Output:
Core Java
Advanced Java

C:\>java Demo>a.txt

Output:
Advanced Java
C:\>start notepad a.txt
Core Java appears in a a.txt file because Core Java 
written with System.out 
This stream data can be redirected to a file.

File Streams:

1) FileInputStream
2) FileOutputStream

FileInputStream used to read data from file.

FileOutputStream used to write data to a file.

Program to read data from file:

class ReadDemo
{

         public static void main(String args[])
         {
               try{
               FileInputStream fis=new FileInputStream(args[0]);
               int n=fis.available();
               byte[] b=new byte[n];
               fis.read(b);
               String s =new String(b);
               System.out.println(s);
               }catch(Exception e)
               {
                     System.err.println(e);
               } 
         }
}

Program to copy data from one file to another file:

class CopyDemo
{

         public static void main(String args[])
         {
               try{
               FileInputStream fis=new FileInputStream(args[0]);
               int n=fis.available();
               byte[] b=new byte[n];
               fis.read(b);
               FileOutputStream fos=
                                               new FileOutputStream(args[1]);
               fout.write(b);
               }catch(Exception e)
               {
                     System.err.println(e);
               } 
         }
}

There are two types of streams:
1) Byte streams
2) Character streams

Byte streams handle all types of data where as character

streams handle text only.

List of byte stream classes:

1) InputStream
2) PrintStream
3) FileInputStream
4) FileOutputStream
5) ObjectInputStream
6) ObjectOutputStream
7) DataInputStream
8) DataOutputStream

DataInputStream & DataOutputStream classes support

all primitive data types & strings.

ObjectInputStream & ObjectOutputStream classes 

support objects

List of Character streams:

1) FileReader
2) FileWriter
3) BufferedReader
4) BufferedWriter

Finally block:

It is used to perform cleanup activities.
Clean up activities are closing a file, closing a database
connection, closing a socket, ..etc.,
Finally block is executed even exception occurs in a 
program.

Example:

class ReadDemo
{

         public static void main(String args[])
         {
               FileInputStream fis=null;
               try{
               fis=new FileInputStream(args[0]);
               int n=fis.available();
               byte[] b=new byte[n];
               fis.read(b);
               String s =new String(b);
               System.out.println(s);
               }catch(Exception e)
               {
                     System.err.println(e);
               } 
               finally
               {
                         try{
                         fis.close();
                         }catch(Exception e)
                         {
                                System.err.println(e);
                          }
                }
         }
}

Object Streams:

1) ObjectInputStream:
It is used to read an object from file

2) ObjectOutputStream:

It is used to write an object to a file

Serialization:

It is a process of converting object into a series of bits.
In Java, object must be serializable to do the 
following operations:
1) Writing object to a file
2) Reading object from file
3) Writing object to a network
4) Reading object from network

The class must implements java.io.Serializable interface 

to make serializable object 

java.io.Serializable interface is a marker interface, 

tag interface or empty interface because no members 
in this interface.

Example:

import java.io.*;
class Emp implements Serializable
{
transient int empNo=101;
float salary=5000.00f;
}
class Demo
{
          public static void main(String args[])
          {
try{
Emp e1=new Emp();
FileOutputStream fos=
                                  new FileOutputStream("emp.txt");
ObjectOutputStream oos=
                                           new ObjectOutputStream(fos);
oos.writeObject(e1);
oos.close();
fos.close();
FileInputStream fis=
                                        new FileInputStream("emp.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Emp e2=(Emp)ois.readObject();
System.out.println(e2.empNo+"\t"+e2.salary);
ois.close();
fis.close();
}catch(Exception e)
{
System.err.println(e);
}
          }
}
transient keyword:
It is used to prevent serialization. It is used in real 
time applications with passwords, PIN numbers, 
security code, ... etc.,