0% found this document useful (0 votes)
2 views9 pages

IO Java

The document provides an overview of IO Streams in Java, detailing the two main types: Byte Streams and Character Streams. It explains various classes for reading and writing data, including FileInputStream, FileOutputStream, FileReader, and FileWriter, along with examples of their usage. Additionally, it covers Deflater and Inflater Streams for data compression and uncompression, highlighting the relevant classes and syntax.

Uploaded by

itgetachew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

IO Java

The document provides an overview of IO Streams in Java, detailing the two main types: Byte Streams and Character Streams. It explains various classes for reading and writing data, including FileInputStream, FileOutputStream, FileReader, and FileWriter, along with examples of their usage. Additionally, it covers Deflater and Inflater Streams for data compression and uncompression, highlighting the relevant classes and syntax.

Uploaded by

itgetachew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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

1.1.Input 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.

1.2.OutputStream: 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“java.io.package”.

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 java.io.*; //reading multiple characters and


displaying then
class Readdemo{
int ch;
public static void main(String[] args)
throws IOException{ while((ch = dis.read())!=-1){

//Creating the object of System.out.println((char) ch);


DataInputStream and Connecting it
}
to the resource.
}
DataInputStream dis = new
DataInputStream(System.in); }
input.text(file)

FileInputStream

//Reading a Array(byte) BufferedInputStream

import java.io.*;

*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 java.util.*; //connecting FileInputStream to a


file
class FileRead{
FileInputStream fis = new
public static void main(String[]
FileInputStream(“input.txt”);
args){

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

*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 java.io.*; while((ch = fis.read())! = -1){

class Filecopy{ fos.write(ch);

public static void main(String[] args) }


throws Exception{
fis.close();
FileInputStream fis = new
fos.close();
FileInputStream(“input.txt”);
}
FileOutputStream fos = new
FileOutputStream(“output.txt”,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 java.io.*; while((ch = fr.read())! = -1){

class FileReaderDemo{ System.out.print((char)ch);

public static void main(String[] }


args)throws IOException{ fr.close();

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

FileWriter fw = new FileWriter(fileName, boolean); //append


import java.util.*;

4|Page
class FileWriterdemo{ fw.write(c);

public static void main(Stirng[] args) for(int i =0; i<str.length(); i++){


throws IOException{
fw.write(str.charAt(i));
FileWriter fw = new
}
FileWriter(“abc.txt”);
fw.close();
String str =
“abcdefghijklmnopqrstuvwxyz”; }
fw.writer(str); }
char[] c = str.tocharArray();

***//program write using read a without IO package. ***

import java.lang.*; System.out.print(ch); //write

class demo{ }

public static void main(String[] args) }


throws Exception{

char ch = (char) System.in.read();


//read
***//using with do…while loop ***

***//program can read multiple character from keyboard.***

import java.util.*; ch =(char) System.in.read();

class demo2{ System.out.print(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 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:

InputStreamReader is a class that can be use for connecting a ByteStream and


CharacterStream.

System.in InputStream //in = object, inputstream = class

System.out PrintStream //out = object, printstream = class

System.out.println(); //system = java.lang package, out = reference variable object of PSC

*System.out.println: 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.

System.out 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 system.out.println.

import java.util.*; System.SetOut(ps);

class demo{ System.out.println(“hi”);

public static void main(String[] args) System.out.println(“h r u”);


throws IOException{
System.out.println(“r u listening?”);
FileoutputStream fos = new
}
FileOutputStream(“divert.txt”);
}
printStream ps = new
printStream(fos);

*DeflaterStreams:(java.util.Zip.*;) 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


java.util.zip.*; package.

7|Page
//program to compress the data

import java.io.*; int ch;

import java.util.zip.*; while((ch = fis.read())! = -1){

class Compressdata{ dos.write(ch);

public static void main(String[] args) }


throws IOException{
fis.close();
FileInputStream fis = new
dos.close();
FileInputStream(“input.txt”);
}
FileOutputStream fos = new
FileOutputStream(“temp.txt”); }
DeflaterOutputStream dos = new
DeflaterOutputStream(fos);

Compressed data

input.txt FileInputStream FOS temp.txt

DeflaterOutputStream(1.6 version)

//program uncompressed the data

import java.util.*; FileOutputStream fos = new


FileOutputStream(“output.txt”);
import java.util.*;
int ch;
class Uncompressdata{
while((ch = iis.read())! = -1){
public static void main(String[] args)
throws IOException{ fos.write(ch); {

FileInputStream fis = new iis.close();


FileInputStream(“temp.txt”);
fos.close();
InflaterInputStream iis = new
}
InflaterInputStream(fis);
}

8|Page
9|Page

You might also like