Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - OutputStream Class



Introduction

The Java OutputStream class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink.Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output.

Class declaration

Following is the declaration for Java.io.OutputStream class −

public abstract class OutputStream
   extends Object
      implements Closeable, Flushable

Class constructors

Sr.No. Constructor & Description
1

OutputStream()

Single Constructor.

Class methods

Sr.No. Method & Description
1 void close()

This method closes this output stream and releases any system resources associated with this stream.

2 void flush()

This method flushes this output stream and forces any buffered output bytes to be written out.

3 void write(byte[] b)

This method writes b.length bytes from the specified byte array to this output stream.

4 void write(byte[] b, int off, int len)

This method writes len bytes from the specified byte array starting at offset off to this output stream.

5 abstract void write(int b)

This method writes the specified byte to this output stream.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Object

Example - Using flush() with FileOutputStream

The following example shows the usage of OutputStream flush() method.

OutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;

public class OutputStreamDemo {
   public static void main(String[] args) {
      try {
         FileOutputStream fos = new FileOutputStream("example1.txt");

         String data = "Hello, this is a flush example!";
         fos.write(data.getBytes());

         // Flush the output stream to ensure data is written to file
         fos.flush();

         fos.close();
         System.out.println("Data written and flushed to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Data written and flushed to file.

Explanation

  • write() writes the data to an internal buffer.

  • flush() forces any buffered bytes to be written to the file "example1.txt".

  • Without flush(), if the stream wasn't closed properly, some data might remain in the buffer and not reach the file.

Example - Writing to a file using FileOutputStream and closing it

The following example shows the usage of OutputStream close() method. We're writing data to a file, then explicitly close the stream.

OutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class OutputStreamDemo {
   public static void main(String[] args) {
      OutputStream os = null;
      try {
         os = new FileOutputStream("example1.txt");
         String data = "Hello, OutputStream!";
         os.write(data.getBytes());
         System.out.println("Data written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         try {
            if (os != null) {
               os.close(); // important to close the stream
               System.out.println("Stream closed.");
            }
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Data written to file.
Stream closed.

Explanation

  • os.close() ensures the file is properly saved and the resource is released.

  • Not closing could lead to data not being written or file locks.

Example - Writing a byte array to a file using FileOutputStream

The following example shows the usage of OutputStream write(byte[] b) method.

OutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;

public class OutputStreamDemo {
   public static void main(String[] args) {
      try {
         FileOutputStream fos = new FileOutputStream("output1.txt");

         String data = "This is an example using write(byte[] b)";
         byte[] byteArray = data.getBytes();  // Convert string to byte array

         fos.write(byteArray);  // Write entire byte array to the file
         fos.close();

         System.out.println("Data written to output1.txt");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Data written to output1.txt

Explanation

  • getBytes() converts a string into a byte array.

  • write(byte[] b) writes the entire byte array to the file output1.txt.

  • Efficient for writing strings or binary data at once.

Advertisements