Professional Documents
Culture Documents
===========
- Stream is a via media by which we can read the data from the Source and Write the
data to the Sink.
A. Source : Source is the one which can produce the data like : Keyboard, File ,
Stream
B. Sink : Sink is the one which can consume the data like : File , Monitor ,
Stream
- Depending on the kind of data stream it can be divided into majorly two different
categories they are
1. Binary Stream : This is the stream which will read or write the data using
binary i.e byte by byte.
2. Character Stream : This is the stream which will read or write the data
using character by character.
1. Binary Stream
=================
Again the binary stream can be divided into two different types depending on
reading and writing. They are
1. InputStream 2.OutputStream
- These two are the super class hierarchy of all reading and writing streams.
1. InputStream
================
- This is the super class hiearchy of all the binary reading streams
- Some of the classes which are derived from this particular class they are
1. FileInputStream : This stream is used when we want to read the data from a
File.
Demo : Write a Simple Program to Read a text File from the File System.
========================================================================
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
try {
//FileInputStream() constructor throws an exception of type
FileNotFoundException
FileInputStream fileInputStream = new FileInputStream("C:\\WAVE20\\
Info.txt");
int avlBytes;
//available() method throws an exception of type IOException
avlBytes=fileInputStream.available();
//Reading the data from the file using stream (FileInputStream) and
store into a byte array
fileInputStream.read(content,0,avlBytes);
fileInputStream.close();
}
catch(FileNotFoundException fe){
System.out.println("File Not Found Exception Occured:"+fe);
}
catch(IOException ie){
System.out.println("IO Exception Occured:"+ie);
}
OutputStream
=============
- This is the super class hiearchy of all the binary writing streams.
- From this particular class there are classes which are derived they are
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
try {
FileOutputStream fileOutputStream = new FileOutputStream("C:\\WAVE20\\
content.txt");
System.out.println(strData.length());
for(int i=0;i<strData.length();i++){
fileOutputStream.write(strData.charAt(i));
}
}
}
Demo Program
==============
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
try {
FileOutputStream fileOutputStream = new FileOutputStream("C:\\WAVE20\\
content.txt");
String empData="Sunil:M,Vinod:M,Purvi:F,Rakshita:F,Wasim:M";
//Mr.Sunil,Mr.Vinod,Miss.Purvi,Miss.Rakshita,Mr.Wasim
System.out.println(strData);
for(int i=0;i<strData.length();i++){
fileOutputStream.write(strData.charAt(i));
}
}
}
Demo Program
=============
import java.io.*;
try {
FileInputStream fileInputStream = new FileInputStream("C:\\WAVE20\\
Info.txt");
int avlBytes=fileInputStream.available();
byte content[]=new byte[avlBytes];
fileInputStream.read(content,0,avlBytes);
String empData=new String(content);
for(int i=0;i<strData.length();i++){
fileOutputStream.write(strData.charAt(i));
}
}
}