Java I/O: The OutputStream Methods

Similar to the InputStream class, the abstract OutputStream class is a superclass of all classes representing an output stream of bytes. A few examples of these subclasses are:

·         ByteArrayOutputStream

·         FileOutputStream

·         FilterOutputStream

·         ObjectOutputStream

·         PipedOutputStream

Similar to FileInputStream, the FileOutputStream class provides a real implementation of the OutputStream class.

write Method:                                     

The OutputStream class defines three overloaded write methods:

1.   public abstract void write(int b) throws IOException

2.   public void write(byte[] b) throws IOException

3.   public void write(byte[] b, int off, int len) throws IOException


·      The first method writes the byte specified in its parameter to the output stream. The byte to be written is stored in the eight low-order bits of the argument b. The 24 high-order bits of b are ignored. The subclasses of OutputStream must provide an implementation for this method.

·         The second method writes the entire buffer specified in its parameter

·         Tthe third method writes the buffer contents specified by the first parameter, starting at an offset in the buffer specified by the second parameter, and the number of bytes specified by the third parameter. Note that the actual contents written to the file could be less than the number specified if an error occurs during writing.

flush Method:

The flush method flushes the contents of the buffer to the output stream:

public void flush() throws IOException

·         This method is useful if we want to force an immediate write of the file buffer.

·    If we do not flush the buffer using the flush method, the operating system will at some suitable time write the buffer to the physical file.

·   In the case of multiuser applications, flushing the buffer immediately becomes important for maintaining the consistency of data between different threads or users.

·         Generally, a word processing program such as Microsoft Word flushes our edits into a temporary file periodically so that if the program crashes for some reason, our edits are not completely lost. The explicit save operation by the user flushes all edits to the original file.



We have already used one of the constructors of the FileOutputStream class that takes one String argument specifying the name of the file to be opened. Another important variation of the constructor is the one that takes two parameters, as specified here:

public FileOutputStream(String name, boolean append) throws FileNotFoundException

·         The first parameter specifies the filename, as in the earlier case.

·         The second parameter, if set to true, indicates that the file should be opened in append mode.

·         Any data we write to a file opened in append mode will be added to the tail of the file. Thus, the original contents would be kept intact.

·         In other words, the file is not overwritten when it is opened in append mode. The method throws the FileNotFoundException if the file exists but refers to a folder rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.

·        The method may also throw a SecurityException if a security manager exists and its checkWrite method denies write access to the file.

Leave Comment