Course Content‎ > ‎Session 6‎ > ‎

Byte Stream - OutputStream

Like InputStream, the abstract class OutputStream also provides the fundamental methods used by all its derived classes.  


              Hierarchy of OutputStream class

Given below is the description of the commonly used methods for this class:

Method                  Description                                                                                                    
write(int)              Writes a single byte to the output stream
write(byte [])          Writes an entire length of an array of bytes to the 
                        output stream
write(byte [], int)     Writes a portion of an array of bytes starting at a 
                        specific location in the array for a certain number 
                        of bytes.
flush()                 If the output stream is buffered, this method will 
                        force any bytes in the buffer to be written to the 
                        stream.
close()                 Closes the output stream and releases any resources 
                        allocated by the system.

FileOutputStream

Just like FileInputStream class is used for reading the contents of a file, similarly FileOutputStream is a type of OutputStream, used for writing the data to a file. Object of FileOutputStream class is created using one of the three constructors that accept the name of the file, a File object or a FileDescriptor object respectively.

Given below is the code, which writes the contents, typed through keyboard to the file Demo.txt, until the character ! is encountered:

    import java.io.*;
    public class readFile1
    {
        public static void main(String args[])
        {
            char ch=' ';
            try
            {
                // FileOutputStream object is created
                FileOutputStream fout = new FileOutputStream("Demo.txt");
                
                while(ch != '!')
                {
                    // character typed through keyboard is assigned to ch
                    ch = (char)System.in.read();
                    // the char assigned to ch is written to Demo.txt
                    fout.write(ch); 
                }
            }
            catch(IOException e)
            {
                System.out.println("Error :"+e);
            }
        }
    }


Output of the above code, upon execution will be:

    What do you think about this? Hello!

Demo.txt will show:
    What do you think about this? Hello!

ByteArrayOutputStream
Creates a buffer in memory to store the data, which is sent to the stream.

FilterOutputStream Subclasses

DataOutputStream
This class provides methods that write data to an output stream. Like DataInputStream, this class also implements method of an interface, DataOutput. The DataOutput interface defines the methods to write Java's primitive data types to a source. Within the java.io package, DataOutput is implemented by DataOutputStream and RandomAccessFile.  DataOutputStream implements DataOutput to write primitive types to an output stream.  RandomAccessFile implements DataOutput to write primitive types to a disk file. Methods of DataOutputStream begin with write. Commonly used methods are writeByte (int), writeShort(int), writeLong(long), writeDouble(double), writeFloat(float), writeChar(int), writeInt(int), writeChars(String).

    //This program writes to a file out.txt
    import java.io.*;
    public class writeFile
    {
        public static void main(String args[])
        {
            try
            {
                FileOutputStream fos = new FileOutputStream("c:\\temp\\Out.txt");
                
                //DataOutputStream allows you to write chars 
                DataOutputStream dos = new DataOutputStream(fos);
                dos.writeChars("This is a program to show DataOutputStream");
            }
            catch(IOException e)
            {
                System.out.println("Error:"  +e);
            }
        }
    }
    
Output of the above program upon execution will create an output file called out.txt in c:\temp and have this string in the file:
    This is a program to show DataOutputStream

BufferedOutputStream

Class BufferedOutputStream allows you to write bytes to a stream without an actual file system write. When you flush() the buffer, close the stream, or fill up the internal buffer, the buffer is written to the stream. BufferedOutputStream objects must be attached to OutputStream objects.

PrintStream

PrintStream is based on BufferedOutputStream , which is based on a FileOutputStream with a specified file descriptor.

    import java.io.*;
    public class writeFile2
    {
        public static void main(String args[])
        {
            try
            {
                FileOutputStream fos = new FileOutputStream("writeFile.txt");
                BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);
                PrintStream ps = new PrintStream(bos, false);
                
                // redirect System.out to it
                System.setOut(ps);
                System.out.println("This program shows the use of 
                                   BufferedOutputStream and PrintStream");
                ps.close();
            }
            catch (IOException e)
            {
                System.out.println("Error: " +e);
            }
        }
    }

Output of the above program upon execution will create the file called writeFile.txt in c:\temp (created above):

This program shows the use of BufferedOutputStream and PrintStream

Comments