Output Stream and File Output Stream classes with its methods and example


OutputStream Class:
  • Syntax:
          java.lang.Object
                  java.io.OutputStream
  • This abstract 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.
  • It has one constructor and three methods.
  • Constructor: public OutputStream()
  • Methods:
    • 1. close:
      • Syntax: public void close() throws IOException
      • Use: Closes this output stream and releases any system resources associated with this stream. A closed stream cannot perform output operations and cannot be reopened.
      • Throws: IOException - if an I/O error occurs.
    • 2. flush
      • Syntax: public void flush() throws IOException
      • Use: Flushes this output stream and forces any buffered output bytes to be written out. 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.
      • Throws: IOException - if an I/O error occurs.
    • 3. write
      • Syntax
        • public void write(byte[] b) throws IOException
        • public void write(byte[] b, int off, int len) throws IOException
        • public abstract void write(int b) throws IOException
      • Use
        • Writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length). 
        • Writes len bytes from the specified byte array starting at offset off to this output stream. 
        • Writes the specified byte to this output stream.
      • Parameters: 
        • b - the data.
        • off - the start offset in the data.
        • len - the number of bytes to write.
      • Throws: IOException - if an I/O error occurs.
FileOutputStream Class:

  • Syntax:
          java.lang.Object
                  java.io.OutputStream
                          java.io.FileOutputStream
  • A file output stream is an output stream for writing data to a File or to a FileDescriptor. 
  • FileOutputStream is meant for writing streams of raw bytes such as image data.
  • It has five constructors and five methods.
  • Constructor: 
    • Syntax:
      • public FileOutputStream(File file) throws FileNotFoundException
      • public FileOutputStream(File file, boolean append) throws FileNotFoundException
      • public FileOutputStream(FileDescriptor fdObj)
      • public FileOutputStream(String name) throws FileNotFoundException
      • public FileOutputStream(String name, boolean append) throws FileNotFoundException
    • Use:
      • Creates a file output stream to write to the file represented by the specified File object. 
      • Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.
      • Creates a file output stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.
    • Parameters:
      • file - the file to be opened for writing.
      • append - if true, then bytes will be written to the end of the file rather than the beginning
      • fdObj - the file descriptor to be opened for writing
      • name - the system-dependent filename
    • Throws:
      • FileNotFoundException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason
      • SecurityException - if a security manager exists and its checkWrite method denies write access to the file.
  • Methods:
    • 1. close:
      • It is an overridden method from the OutputStream class.
      • Note: for more details: See OutputStream Class (Methods: 01)
    • 2. finalize:
      • Syntax: protected void finalize() throws IOException
      • Use: Cleans up the connection to the file, and ensures that the close method of this file output stream is called when there are no more references to this stream.
      • Overrides: finalize in class Object
      • Throws: IOException - if an I/O error occurs.
    • 3. getChannel:
      • Syntax:  public FileChannel getChannel()
      • Use: Returns the unique FileChannel object associated with this file output stream.
      • Returns: the file channel associated with this file output stream
    • 4. getFD:
      • Syntax: public final FileDescriptor getFD() throws IOException
      • Use: Returns the file descriptor associated with this stream.
      • Returns: the FileDescriptor object that represents the connection to the file in the file system being used by this FileOutputStream object.
      • Throws: IOException - if an I/O error occurs.
    • 5. write: 
      • It is an overridden method from the OutputStream class.
      • Note: for more details: See OutputStream Class (Methods: 03)
Example (Write File):
  • Step 01: Write a JAVA program.
  • Step 02: Enjoy the Output.
  • Step 03: See the .txt file in Files sections.
Example (Copy File):
  • Step 01: Create a .txt file.
  • Step 02: Write a JAVA program.
  • Step 03: Run program and Enjoy Output.
  • Step 04: See the output in .txt file.

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form