You are on page 1of 5

Stream

===========
- 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

- In java everything is a device so writing and reading has to be done to the


device or from the device.

- Depending on the kind of data stream it can be divided into majorly two different
categories they are

1. Binary Stream 2.Character Stream

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

- This is the abstract class which is available in java.io package.

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

2. ObjectInputStream : This stream is used when we want to read an object 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;

public class FileReading {


public static void main(String[] args) {

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();

System.out.println("# of Bytes available in the File :"+avlBytes);

//# of bytes avilable based on this we will create an byte array so


that reading data can be stored.
byte content[]=new byte[avlBytes];

//Reading the data from the file using stream (FileInputStream) and
store into a byte array
fileInputStream.read(content,0,avlBytes);

//Converting a byte array into a String


String byteContentString=new String(content);

System.out.println("Reading String is :"+byteContentString);

fileInputStream.close();
}
catch(FileNotFoundException fe){
System.out.println("File Not Found Exception Occured:"+fe);
}
catch(IOException ie){
System.out.println("IO Exception Occured:"+ie);
}

System.out.println("File Reading Operation");


}
}

OutputStream
=============
- This is the super class hiearchy of all the binary writing streams.

- This is the abstract class which is available in java.io package.

- From this particular class there are classes which are derived they are

1.FileOutputStream 2.ObjectOutputStream 3.ByteArrayOutputStream

Demo Program : Write a Simple Program to print a String to File

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileWriting {


public static void main(String[] args) {

try {
FileOutputStream fileOutputStream = new FileOutputStream("C:\\WAVE20\\
content.txt");

String strData="Hello I am learning java full stack development";

System.out.println(strData.length());

for(int i=0;i<strData.length();i++){

fileOutputStream.write(strData.charAt(i));
}

System.out.println("The Content Written to File");


fileOutputStream.close();
}
catch (FileNotFoundException fe){
System.out.println("File Not Found in File System"+fe);
}
catch (IOException ie){
System.out.println("IOException Occured"+ie);
}

}
}

Demo Program
==============
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileWriting {

public static String convert(String data)


{
String tempData="";
String eachEmployee[]=data.split(",");
for(String eachEmp:eachEmployee){
System.out.println(eachEmp);
String empDetail[]=eachEmp.split(":");
String empName=empDetail[0];
String gender=empDetail[1];
if(gender.equals("M")) {
String empData = "Mr."+empName+",";
System.out.println(empData);
tempData=tempData+empData;
}
else {
String empData = "Miss."+empName+",";
System.out.println(empData);
tempData=tempData+empData;
}
}
return tempData;
}
public static void main(String[] args) {

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

String strData= FileWriting.convert(empData);

//String strData="Hello I am learning java full stack development";

System.out.println(strData);

for(int i=0;i<strData.length();i++){

fileOutputStream.write(strData.charAt(i));
}

System.out.println("The Content Written to File");


fileOutputStream.close();
}
catch (FileNotFoundException fe){
System.out.println("File Not Found in File System"+fe);
}
catch (IOException ie){
System.out.println("IOException Occured"+ie);
}

}
}

Demo Program
=============
import java.io.*;

public class FileWriting {

public static String convert(String data)


{
String tempData="";
String eachEmployee[]=data.split(",");
for(String eachEmp:eachEmployee){
System.out.println(eachEmp);
String empDetail[]=eachEmp.split(":");
String empName=empDetail[0];
String gender=empDetail[1];
if(gender.equals("M")) {
String empData = "Mr."+empName+",";
System.out.println(empData);
tempData=tempData+empData;
}
else {
String empData = "Miss."+empName+",";
System.out.println(empData);
tempData=tempData+empData;
}
}
return tempData;
}
public static void main(String[] args) {

try {
FileInputStream fileInputStream = new FileInputStream("C:\\WAVE20\\
Info.txt");

FileOutputStream fileOutputStream = new FileOutputStream("C:\\WAVE20\\


content.txt");

int avlBytes=fileInputStream.available();
byte content[]=new byte[avlBytes];
fileInputStream.read(content,0,avlBytes);
String empData=new String(content);

String strData= FileWriting.convert(empData);

for(int i=0;i<strData.length();i++){

fileOutputStream.write(strData.charAt(i));
}

System.out.println("The Content Written to File");


fileOutputStream.close();
fileInputStream.close();
}
catch (FileNotFoundException fe){
System.out.println("File Not Found in File System"+fe);
}
catch (IOException ie){
System.out.println("IOException Occured"+ie);
}

}
}

You might also like