Stream is way of access data. In Java, streams are the sequence of data that are read from the source and written to the destination.

An input stream is used to read data from the source. And, an output stream is used to write data to the destination.

Types of Streams

Depending upon the data a stream holds, it can be classified into:

        Byte Stream

        Character Stream


Byte Streams

Java byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are

    FileInputStream 

    FileOutputStream


Character Streams

Java Byte streams are used to perform input and output of 8-bit bytes, whereas Java Character streams are used to perform input and output for 16-bit unicode. Though there are many classes related to character streams but the most frequently used classes are

    FileReader

    FileWriter


Byte Streams

File Input Stream / Read File

Thea InputStrem class of the java.io package is an abstract superclass that represents an input stream of bytes.

Since InputStrem is an abstract class, it is not useful by itself. However, its subclasses can be used to read data.

Example -
import java.io.*;
public class Stream{
public static void main(String []arr)throws IOException{
try{
FileInputStream fr=new FileInputStream("stream.txt");
int i;
while(true){
i=fr.read();
if(i==-1){
break;
}
System.out.print((char)i);
}
fr.close();
}
catch(FileNotFoundException e){
System.out.print("File not exist");
}
}
}
Output -
    Hello Hanuman Siyag


File Output Stream / Write File 

The OutputStream class of the java.io package is an abstract superclass that represents an output stream of bytes.

Since OutputStream is an abstract class, it is not useful by itself. However, its subclasses can be used to write data.

Example -
import java.io.*;
class Stream{
public static void main(String []arr)throws IOException{
try{
FileOutputStream fout=new FileOutputStream("stream.txt");
PrintStream pw=new PrintStream(fout);
pw.print("Hello");
}
catch(FileNotFoundException e){
System.out.print("File not exist");
}
}
}
Output -
Hello

Copy File 

Copy one file and paste another file

Example -
import java.io.*;
class Stream{
public static void main(String []arr)throws IOException{
try{
FileInputStream fr=new FileInputStream("stream.txt");
FileOutputStream fout=new FileOutputStream("stream1.txt");
PrintStream pr=new PrintStream(fout);
int i;
while(true){
i=fr.read();
if(i==-1){
break;
}
pr.print((char)i);
}
fr.close();
pr.close();
}
catch(FileNotFoundException e){
System.out.print("File not exist");
}
}
}
Output - 
    Hello


Question : 

    1.  Write a program to line count.
     2.  Write a program to word count.
     3.  Write a program to reverse word display.
     4.  Write a program to all first character is capital. 


Character Streams

FileReader / String Read / BufferedReader

The Bufferedreader class of the java.io package can be used with other readers to read data (in characters) more efficiently.

It extends the abstract class Reader.

In StringReader, the specified string acts as a source from where characters are read individually.

The Bufferedreader maintains an internal buffer of 8192 characters.

During the read operation in Bufferedreader a chunk of characters is read from the disk and stored in the internal buffer. And from the internal buffer characters are read individually.

Hence, the number of communication to the disk is reduced. This is why reading characters is faster using  Bufferedreader.

Example -
import java.io.*;
class Demo{
public static void main(String []arr)throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
System.out.print(s);
}
}
Output -
Input - Hello
Output - Hello

Example -
import java.io.*;
class Demo{
public static void main(String []arr)throws IOException{
try{
BufferedReader br=new BufferedReader(new FileReader("string.txt"));
String s=null;
while(true){
s=br.readLine();
if(s==null){
break;
}
System.out.print(s);
}
br.close();
}
catch(FileNotFoundException e){
System.out.print("File Not Foiund");
}
}
}
Output -
Hello guy's my name is Hanuman Siyag


FileWriter / String Write / BufferedWrite

The BufferedWrite class of the java.io package can be used with other writers to write data (in characters) more efficiently.

It extends the abstract class Write.

In Buffered Reader send reader class object as a argument.

The BufferedWrite  maintains an internal buffer of 8192 characters.

During the write operation, the characters are written to the internal buffer instead of the disk. Once the buffer is filled or the writer is closed, the whole characters in the buffer are written to the disk.

Hence, the number of communication to the disk is reduced. This is why writing characters is faster using BufferedWrite .

In Java, string buffer is considered as a mutable string. That is, we can modify the string buffer. To convert from string buffer to string, we can use the toString() method.

Create a New File

Example -
import java.io.*;
class Demo{
public static void main(String []arr)throws IOException{
File f=new File("xyz.txt");
f.createNewFile();
}
}


Print File & Folder

Example -
import java.io.*;
class Demo{
public static void main(String []arr)throws IOException{
String path = "C:/Program Files/Java/java program/Exception Handling/Buffered Reader";
File f=new File(path);
String s[] = f.list();
for(int i=0; i<s.length; i++){
String cpath = path+"/"+s[i];
File fr = new File(cpath);
if(fr.isDirectory()){
System.out.print("Folder :" +cpath);
}
else if(fr.isFile()){
System.out.print("File :" +cpath);
}
}
}
} 

 

Question : 

    1.  Write a program to count all text file in a folder.
     2.  Write a program to count all png and jpg file in a folder
     3.  Write a program to ount all java file in a folder.
     4.  Write a program to read a path from user and search a string (an) and print.