Use of ByteStreams and CharacterStreams in JAVA

ByteStreams And CharacterStreams

 
A stream can represent many different kinds of sources and destinations including disk file, device, other program and memory arrays. An I/o stream represents an source or an output destination. Stream support many kinds of data including a simple byte, primitive data types, localized characters, and objects. Some Streams simply pass on data, other manipulation and transform the data in useful ways.
 
No matter how they work internally all streams present the same simple model to program that use them: a stream is a sequence of data.
 
Program use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from inputStream or outputStream. there are many byte stream classes. To demonstrate how byte stream work, we will focus on the file I/O byte streams, FileInputStream and FileOutputStream. Other kind of Byte Stream are used much the way; they differ mainly in the way they are constructed.
 
Example of ByteStreams
  1. import java.io.FileInputStream;  
  2. import java.io.FileOutputStream;  
  3. import java.io.IOException;  
  4. public class MyByteStream  
  5. {  
  6.      public static void main(String arg[]) throws IOException  
  7.      {  
  8.           FileInputStream fin=null;  
  9.           FileOutputStream fout=null;  
  10.           try  
  11.           {  
  12.               fin=new FileInputStream("input.txt");  
  13.               fout=new FileOutputStream("output.txt");  
  14.               int c;  
  15.               while((c=fin.read())!=-1)  
  16.               {  
  17.                    fout.write(c);  
  18.               }  
  19.           }  
  20.           finally  
  21.           {  
  22.                if(fin!=null)  
  23.                {  
  24.                     fin.close();  
  25.                }  
  26.                if(fout!=null)  
  27.                {  
  28.                     fout.close();  
  29.                }  
  30.           }  
  31.      }  
  32. }  
Output
 
Before compilation, the output file is blank but after compilation the content of input file copy into the output.
 
byteStreamfile.gif 
 
ByteStreamcmd.gif 
 
After compilation all the content input file you can see in the output file. In this program we are using ByteStream.
 
byteAftercompile.gif 
 

Character Streams

 
All character Stream classes are descended from Reader and Writer. As with Byte streams there are character stream classes that specialize in File I/O:FileReader and FileWriter.
 
Example
  1. import java.io.FileReader;  
  2. import java.io.FileWriter;  
  3. import java.io.IOException;  
  4. public class MyCharacterStream  
  5. {  
  6.      public static void main(String arg[]) throws IOException  
  7.      {  
  8.           FileReader fin=null;  
  9.           FileWriter fout=null;  
  10.           try  
  11.           {  
  12.                fin=new FileReader("input.txt");  
  13.                fout=new FileWriter("output.txt");  
  14.                int c;  
  15.                while((c=fin.read())!=-1)  
  16.                {  
  17.                    fout.write(c);  
  18.                }  
  19.           }  
  20.           finally  
  21.           {  
  22.                 if(fin!=null)  
  23.                 {  
  24.                      fin.close();  
  25.                 }  
  26.                 if(fout!=null)  
  27.                 {  
  28.                      fout.close();  
  29.                 }  
  30.           }  
  31.      }  
  32. }  
Output
 
Before compilation initial state of input output file. The output File is initially blank.
 
CHARACTERSTREAM.gif 
 
Mycharacterstreamcmd.gif 
 
After compilation all the content of input file is copying into the output file. In this copy program we are using character stream.
 
CHARCTERSTREAMAFTER.gif 
 
Resources


Similar Articles