**IO STREAMS**
*IO Stream: Stream are used to perform sequential flow of data. Sequential of
data one location to and other location. The streams are classified into two
types. They are:
1. Byte Streams
2. Character Streams
1. Byte Streams: (Byte by Byte) The Streams that will the perform the
operations Byte by Byte are called as ‘Byte Streams’. These Streams can
handle any kind of data like Text, Audio, Video, Images etc. The Byte
Streams are further classified into two categories.
[Link] Stream: These Streams are used to perform Reading operation
from any resource using these Streams we can read data into an application.
Ex: File InputStream
Data InputStream etc.
[Link]: These Streams are used to perform writing operation to
any resource. These Streams can he used to send data out of the applications.
Ex: FileOutputStream
BufferedOutputStream etc.
2. Character Streams: (char by char)The Streams that perform the
operation character by character are called as character Streams. These
Streams can handle only text. There also called as Text Streams. The
character Streams are faster then Byte Streams. The character Streams are
further classified into two catagories.
2.1. Reader: These Streams are similar to Input Streams performing
Reading operations from various resources.
Ex: FileReader
BufferedReader
2.2. Writer: These Streams are similar to output Streams
performing writing operations on to various resources.
Ex: FileWriter
PrintWriter
All the Stream related classes are available in“[Link]”.
1|Page
*DataInputStream: This Stream is used to perform Reading operation from
any resource.
Creation of DataInputStream
Syntax: DataInputStream dis = new DataInputStream(resource);
//pro from Read to keyboard.
import [Link].*; //reading multiple characters and
displaying then
class Readdemo{
int ch;
public static void main(String[] args)
throws IOException{ while((ch = [Link]())!=-1){
//Creating the object of [Link]((char) ch);
DataInputStream and Connecting it
}
to the resource.
}
DataInputStream dis = new
DataInputStream([Link]); }
[Link](file)
FileInputStream
//Reading a Array(byte) BufferedInputStream
import [Link].*;
*FileInputStream: This Stream is used to Read the contents from a file.
Syntax: FileInputStream fis = new FileInputStream(fileName);
If the fileName that is specified is not available then we get a runtime Exception
called file not found Exception.
import [Link].*; //connecting FileInputStream to a
file
class FileRead{
FileInputStream fis = new
public static void main(String[]
FileInputStream(“[Link]”);
args){
2|Page
//connection BufferedInputStream to [Link]((char)ch);
FileInputStream
}
BufferedInputStream bis = new
//releasing the resources
BufferedInputStream(fis);
[Link]();
//reading data from a file and
displaying }
int ch; }
while((ch = [Link]())! = -1){
[Link] [Link]
FIS FOS
*FileOutputStream: This calss is used to write the contents into a file.
Syntax:
FileOutputStream fos = new FileOutputStream(fileName);
//This Syntax will overwrite the contents.
FileOutputStream fos = new FileOutputStream(fileName, boolean);
//This Syntax will append the contents.
3|Page
import [Link].*; while((ch = [Link]())! = -1){
class Filecopy{ [Link](ch);
public static void main(String[] args) }
throws Exception{
[Link]();
FileInputStream fis = new
[Link]();
FileInputStream(“[Link]”);
}
FileOutputStream fos = new
FileOutputStream(“[Link]”,true); }
int ch;
Note: If the output file i.e. specified is not available then we do not get file not
found Exception nested a file with the specified name will be created
automatically.
*FileReader: This class is used to Read the content form a file ‘Char by Char’.
Cretion of file Reader.
Syntax: FileReader fr = new FileReader(fileName);
import [Link].*; while((ch = [Link]())! = -1){
class FileReaderDemo{ [Link]((char)ch);
public static void main(String[] }
args)throws IOException{ [Link]();
FileReader fr = new }
FileReader(“[Link]”);
}
int ch;
*FileWriter: This file is use to into write the contents into a file character by
character. Creation of filewriter.
Syntax:
FileWriter fw = new FileWriter(fileName);//over writing
FileWriter fw = new FileWriter(fileName, boolean); //append
import [Link].*;
4|Page
class FileWriterdemo{ [Link](c);
public static void main(Stirng[] args) for(int i =0; i<[Link](); i++){
throws IOException{
[Link]([Link](i));
FileWriter fw = new
}
FileWriter(“[Link]”);
[Link]();
String str =
“abcdefghijklmnopqrstuvwxyz”; }
[Link](str); }
char[] c = [Link]();
***//program write using read a without IO package. ***
import [Link].*; [Link](ch); //write
class demo{ }
public static void main(String[] args) }
throws Exception{
char ch = (char) [Link]();
//read
***//using with do…while loop ***
***//program can read multiple character from keyboard.***
import [Link].*; ch =(char) [Link]();
class demo2{ [Link](ch);
public static void main(String[] args) }
throws Exception{
while(ch! = -1);
char ch;
}
do{
}
5|Page
***//A program to read to line of data.***
import [Link].*;
(new
class demo{
InputStreamReader([Link]));
public static void main(String[] args)
String str = [Link]();
throws IOException{
[Link](str);
BufferedReader br = new
BufferedReader }
Note:
InputStreamReader is a class that can be use for connecting a ByteStream and
CharacterStream.
[Link] InputStream //in = object, inputstream = class
[Link] PrintStream //out = object, printstream = class
[Link](); //system = [Link] package, out = reference variable object of PSC
*[Link]: out is a reference variable of print Stream class. Pointing
to the object of printStream class declared as static inside System class.
A static reference variable presenting any class can be access directly by using
class name.
[Link] will give us the object of printStream class, with that object we can
call methods of printStream class.
Println method belong to printStream class.
6|Page
//program to derect the connects into a file by using [Link].
import [Link].*; [Link](ps);
class demo{ [Link](“hi”);
public static void main(String[] args) [Link](“h r u”);
throws IOException{
[Link](“r u listening?”);
FileoutputStream fos = new
}
FileOutputStream(“[Link]”);
}
printStream ps = new
printStream(fos);
*DeflaterStreams:([Link].*;) package:
DeflaterStreams these streams are used meant for compressing the data.
We can compress the data by reading the or writing the data.
DeflaterInputStream & DeflaterOutputStream both are meant for compressing
the data.
Syntax:
DeflaterInputStream dis = new DeflaterInputStream(InputStream);
DeflaterOutputStream dos = new DeflaterOutputStream(OutputStream);
*InflaterStreams(uncompressing):
* These Streams are used for uncompressing the data.
* We can uncompress the data while reading or while writing.
Syntax:
InflaterInputStream iis = new InflaterInputStream(InputStream);
InflaterOutputStream ios = new InflaterOutputStream(OutputStream);
All the InflaterStreams & DeflaterStreams are available in “import
[Link].*; package.
7|Page
//program to compress the data
import [Link].*; int ch;
import [Link].*; while((ch = [Link]())! = -1){
class Compressdata{ [Link](ch);
public static void main(String[] args) }
throws IOException{
[Link]();
FileInputStream fis = new
[Link]();
FileInputStream(“[Link]”);
}
FileOutputStream fos = new
FileOutputStream(“[Link]”); }
DeflaterOutputStream dos = new
DeflaterOutputStream(fos);
Compressed data
[Link] FileInputStream FOS [Link]
DeflaterOutputStream(1.6 version)
//program uncompressed the data
import [Link].*; FileOutputStream fos = new
FileOutputStream(“[Link]”);
import [Link].*;
int ch;
class Uncompressdata{
while((ch = [Link]())! = -1){
public static void main(String[] args)
throws IOException{ [Link](ch); {
FileInputStream fis = new [Link]();
FileInputStream(“[Link]”);
[Link]();
InflaterInputStream iis = new
}
InflaterInputStream(fis);
}
8|Page
9|Page