Use of ByteStreams and CharacterStreams in JAVA


ByteStreams And CharacterStreams

A steam 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 kind of data  including a simple bytes, 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 Byte Streams

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MyByteStream
{
     public static void main(String arg[]) throws IOException
     {
          FileInputStream fin=null;
          FileOutputStream fout=null;
          try
          {
              fin=new FileInputStream("input.txt");
              fout=new FileOutputStream("output.txt");
              int c;
              while((c=fin.read())!=-1)
              {
                   fout.write(c);
              }
          }
          finally
          {
               if(fin!=null)
               {
                    fin.close();
               }
               if(fout!=null)
               {
                    fout.close();
               }
          }
     }
}

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

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class MyCharacterStream
{
     public static void main(String arg[]) throws IOException
     {
          FileReader fin=null;
          FileWriter fout=null;
          try
          {
               fin=new FileReader("input.txt");
               fout=new FileWriter("output.txt");
               int c;
               while((c=fin.read())!=-1)
               {
                   fout.write(c);
               }
          }
          finally
          {
                if(fin!=null)
                {
                     fin.close();
                }
                if(fout!=null)
                {
                     fout.close();
                }
          }
     }
}

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 in to the output file.In this copy program we are  using character stream.

CHARCTERSTREAMAFTER.gif

Resources

BufferedStream, MemoryStream and CharacterStream Class in C#

Using statement in C#

Creating Excel/Word/HTML document using C#

Up Next
    Ebook Download
    View all
    Learn
    View all