Java.io - SPLessons

Java.io OutputStream

Home > Lesson > Chapter 34
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io OutputStream

Java.io OutputStream

shape Introduction

The Java.io OutputStream is one of the supper class to all types of output streams in java.io package. Generally the output stream acts as a destination to the data like file or connection.

shape Conceptual figure

The ByteArrayOutputStream, FileOutputStream, FilterOutputStream, are the subclass of Output stream and shown in detail in the above figure.

shape Description

The methods inherited mainly from the OutputStream class to subclasses are write(), flush() and close(). All the subclasses to output stream supports all this methods.

Class Declaration

shape Declaration

Java.io OutputStream class is declared below: Public abstract class OutputStream extends Object implements closeable, flushable

Class Constructors

shape Table

Constructor Description
OutputStream() The function of this constructor is to create the output stream instance.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released without any impact.
void flush () The function of this method is to flush the specified output stream.
abstract void write (int b) The function of this method is to write the represented byte into output stream.
void write (byte[] b, int off, int n) By using this method it writes the data up to 'n' bytes from the output stream to an array of bytes.

Inherited Methods

shape Description

From the following classes, methods are inherited to output stream class.
  • Java.io.Object

shape Examples

Usage of void close() method. [c]import java.io.*; public class OutputStream { public static void main(String[] args) { try { // creating a new output stream FileOutputStream os = new FileOutputStream("test.txt"); // creating a new input stream InputStream is = new FileInputStream("test.txt"); // write anything you want os.write('I'); // flush the stream os.flush(); // close the stream but it do nothing os.close(); // read what we want to wrote System.out.println("" + (char) is.read()); } catch (Exception ex) { ex.printStackTrace(); } } }[/c] Output The result will be as follows. [c]I[/c] Usage of void flush() method. [c]import java.io.*; public class OutputStream { public static void main(String[] args) { try { // creating a new output stream FileOutputStream os = new FileOutputStream("test.txt"); // creating a new input stream InputStream is = new FileInputStream("test.txt"); // write anything you want os.write('A'); // flush the stream but it do nothing os.flush(); // write something else os.write('B'); // read the content what we wrote System.out.println("" + is.available()); } catch (Exception ex) { ex.printStackTrace(); } } }[/c] Output The result will be as follows. [c]2[/c]

Summary

shape Key Points

  • The Java.io OutputStream is one of the supper class to all types of output streams in java.io package.
  • Generally the Java.io OutputStream acts as a destination to the data like file or connection.
  • The methods inherited mainly from the OutputStream class to subclasses are write(), flush() and close().