Topics Index

Input/Output: Exploring > Byte Streams >
Siva Nookala - 15 Apr 2016
OutputStream is an abstract superclass of all classes representing an output stream of bytes. The subclass which inherit from OutputStream are ByteArrayOutputStream, FileOutputStream, FilterOutputStream, ObjectOutputStream, OutputStream, PipedOutputStream. These OutputStream sub classes are used to write bytes of data.

public abstract class OutputStream
extends Object
implements Closeable, Flushable
Important Methods :
Method Description
abstract void write(int b) This method writes the specified byte to this output stream.
void write(byte[] b) This method writes b.length bytes from the specified byte array to this output stream.
void close() This method closes this output stream and releases any system resources associated with this stream.
class OutputStreamDemo
    public static void main(String[] args) throws IOException
        File f = new File("E:JavaPrograms\\FolderOne\\fileOne.txt"); // LINE A
        OutputStream os = new FileOutputStream(f); // LINE B
        if(f.exists()) // LINE C
            System.out.println("File exists.");
            // LINE D
             byte b[] = {'i',' ','a','m',' ','f','i','l','e','O','n','e','.'};
            // Writing into file fileOne
            System.out.println("File not found.");
        os.close(); // LINE F
        // Reading from fileOne
        InputStream is = new FileInputStream(f);
        int i = 0;
        while((i = != -1)
            System.out.print((char) i);

File exists.
i am fileOne.


At LINE A we have given the path for file w.
At LINE B we have created an FileOutputStream object and passed file f as parameter.
At LINE C we are checking whether file f exists or not.
At LINE D we have created a byte array.
At LINE E we are writing byte array b in to fileOne using FileOutputStream object.
At LINE F we are closing the OutputStream.

  • Write a single character in to fileOne using write(int i) method. See that earlier data in fileOne is erased.
Rest Of Methods :
Method Description
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.
void flush() This method flushes this output stream and forces any buffered output bytes to be written out.

Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.