Java IO Streams

 

JAVA IO STREAMS

 

Streams
Java programs perform I/O operations through streams. A stream is a sequence of data. It is an abstraction that either produces or consumes information. A stream is linked to a physical device by the Java I/O system. All streams represent an input source and an output destination and they behave in the same manner, even if the actual physical devices to which they are linked differ.
 
Byte Streams and Character Streams
Java defines two types of streams: byte streams and character streams. Byte streams are used for handling binary data. Character streams are used for handling input and output of characters in Unicode.

 



IO STREAM

 

Byte streams
Byte streams are defined using two class hierarchies. At the top are two abstract classes InputStream and OutputStream. These two classes have several subclasses to handle different types of physical devices such as disk files, network connections, memory buffers etc., The most important methods of these classes are read() and write() for reading and writing bytes of data.

 



 BYTE STREAM

 

Character streams
Character streams are defined using two class hierarchies. At the top are two abstract classes Reader and Writer. These two classes handle Unicode character streams. The most important methods of these classes are read() and write() for reading and writing characters of data

 



 

CHARACTER STREAM


 

An example file handling program to demonstrate byte and character streams.
// Byte stream
// Using InputStream and OutputStream classes
// File copy program
/*
Note: Byte stream reads data from file in its ASCII form.
Typecast to char when writing to new file.
*/
import java.io.*;
import java.lang.*;
import java.util.*;
 
class IO_Demo
{
     public static void main(String as[]) throws IOException
     {
          FileInputStream fin = null;
          FileOutputStream fout = null;
          int i;
         
          try
          {
              fin = new FileInputStream("ipt.txt");
              fout = new FileOutputStream("opt.txt");
              while(true)
              {
                   i = fin.read();
                   if(i==-1)
                        break;
                   fout.write((char)i);                             
              }
          }
          catch(Exception e)
          {
              System.out.println(e);
          }
          finally
          {
              if(fin!=null)
                   fin.close();
              if(fout!=null)
                   fout.close();
              System.out.println("File copied.");
          }
     }
}
 
// Character stream
// Using Reader and Writer classes
// Read from console and write to file
 
import java.io.*;
 
class RW
{
     public static void main(String as[]) throws IOException
     {
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          FileWriter fw = null;
          try
          {
              fw = new FileWriter("opt.txt");
              int i = 0;
              while(i<3)
              {
                   String str = br.readLine().trim();
                   fw.write(str+"\n");
                   i++;
              }
          }
 
          catch(Exception e)
          {
              System.out.println(e);
          }
          finally
          {
              if(fw!=null)
                   fw.close();  
              System.out.println("\nEnd of program.");
          }
     }
}

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu