OutputStream Class

Start course
1h 46m

In this course, we'll learn about the Java Input/Output subject.

Learning Objectives

  • API and Java I/O
  • OutputStream Class
  • InputStream Class
  • Reader Class
  • Writer Class
  • Serialization, Deserialization, and Non-Serializable objects

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams


  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge

Hi there. In this video, we'll learn about the Java OutputStream and its methods with the help of an example. The OutputStream class is used to transfer data from the program to the data source. Moves data in bytes. This class is included in the Java.io Package. It's an abstract class. Since it's abstract, it cannot be used directly, but we can use this class thanks to the subclasses of this class. This abstract class is the superclass of all classes representing an OutputStream of bytes. An OutputStream 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. The main subclasses of the most known and used OutputStream class are: FileOutputStream, ByteArrayOutputStream, ObjectOutputStream, PrintStream, etc. Now, let's look at the methods used in this class. The first method is the write() method. With the write() method, we can write any data to a file. 

There are three types of this method. The first one is the single parameter write() method. This method writes the specified byte to this OutputStream. Its parameter is a variable of type int. The general contract for this method is that one byte is written to the OutputStream. The byte to be written is the eight low-order bits of argument b. The second one is again the write() method with a single parameter, but this time it takes a byte array as a parameter. It writes bytes as much as the length of the specified byte array to this OutputStream. And the last one is the three parameters write() method. 

The first parameter is the byte array and the type of the second and third parameters are int. The first parameter represents the data to be written. The second parameter represents the startOffset in the data. And the third parameter represents the number of bytes to write. This method writes len bytes from the specified byte array starting at offset [off] to this OutputStream. The general contract for this method is that some of the bytes in the array b are written to the OutputStream in order. Element b, [off], is the first byte written and b [off +

len - 1] is the last byte written by this operation. The write() method of OutputStream calls the write() method of one argument on each of the bytes to be written out. Subclasses are encouraged to override this method and provide a more efficient implementation. If the first parameter, b array, is null, a NullPointerException is thrown. If the second parameter, off, is negative or the third parameter, len, is negative or [off + len] is greater than the length of the array b, then an IndexOutOfBoundsException is thrown. Also, all types of the write() method throws the IOException if an I/O error occurs. In particular, an IOException may be thrown if the OutputStream has been closed. For this, when we use this method, we should use the try and catch block. Now, let's look at the second method. The second method is the flush() method. 

It flushes this Outputstream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that if any bytes are previously written have been buffered by the implementation of the OutputStream, such bytes should immediately be written to their intended destination. If the intended destination of this stream is an abstraction provided by the underlying operating system, for example, a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing. It does not guarantee that they are actually written to a physical device such as a disk drive. The flush() method of OutputStream does nothing. 

And the last method is the close() method. It closes this OutputStream and releases any system resources associated with this stream. Thus, it increases the system efficiency. Also, a closed stream cannot perform output operations and cannot be reopened. The close() method of OutputStream does nothing. For this reason, if you don't use this method, you don't get any errors. But from the perspective of the system efficiency, we use the close() method. Yes, the OutputStream class  is basically like that. Let's take a short break here. In our next lesson, we'll write data to a file on the device using the OutputStream class. See you in the next lesson.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics