Output Stream

Author: Ravi Poswal

Output Stream:- It is an abstract class implemented by all its sub-classes and used to write a byte.

Some common methods are:-

  • public abstract void write(int) throws java.io.IOException; // a byte to the output stream
  • public void write(byte [ ]) throws java.io.IOException; // to output stream, post the byte array.
  • public void flush( ) throws java.io.IOException; // clearing buffer to the output stream
  • public void close( ) throws java.io.IOException; //close the present output stream

Example:-

a. Writing byte to the file

FileOutputStream out=new FileOutputStream(file1.txt);

out.write(65);

out.close();

Code Example

import java.io.*;
public class output
{public static void main(String arr[])
 {int b; 
try
{FileInputStream infile=new FileInputStream("infile.txt");
b=infile.read();
{System.out.print((char)b);}
infile.close();
}
catch(Exception e)
    { System.out.println(e);
}
}
}