IO Java
IO Java
*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.
Ex: FileOutputStream
BufferedOutputStream etc.
1|Page
*DataInputStream: This Stream is used to perform Reading operation from
any resource.
Creation of DataInputStream
FileInputStream
import java.io.*;
If the fileName that is specified is not available then we get a runtime Exception
called file not found Exception.
2|Page
//connection BufferedInputStream to System.out.print((char)ch);
FileInputStream
}
BufferedInputStream bis = new
//releasing the resources
BufferedInputStream(fis);
bis.close();
//reading data from a file and
displaying }
int ch; }
while((ch = bis.read())! = -1){
input.txt output.txt
FIS FOS
Syntax:
3|Page
import java.io.*; while((ch = fis.read())! = -1){
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.
FileReader fr = new }
FileReader(“input.txt”);
}
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
4|Page
class FileWriterdemo{ fw.write(c);
class demo{ }
5|Page
***//A program to read to line of data.***
import java.util.*;
(new
class demo{
InputStreamReader(System.in));
public static void main(String[] args)
String str = br.readLine();
throws IOException{
System.out.println(str);
BufferedReader br = new
BufferedReader }
Note:
A static reference variable presenting any class can be access directly by using
class name.
System.out will give us the object of printStream class, with that object we can
call methods of printStream class.
6|Page
//program to derect the connects into a file by using system.out.println.
*DeflaterStreams:(java.util.Zip.*;) package:
DeflaterStreams these streams are used meant for compressing the data.
Syntax:
DeflaterInputStream dis = new DeflaterInputStream(InputStream);
*InflaterStreams(uncompressing):
Syntax:
InflaterInputStream iis = new InflaterInputStream(InputStream);
7|Page
//program to compress the data
Compressed data
DeflaterOutputStream(1.6 version)
8|Page
9|Page