Java FilterOutputStream flush() Example

FilterOutputStream class flush() method example. This example shows you how to use flush() method.


Syntax is : public void flush() throws IOException
This method flushes this output stream and forces any buffered output bytes to be written out to the stream. The flush method of
FilterOutputStream calls the flush method of its underlying output stream.

Here is the code.

/**
 * @(#) FlushFilterOutputStream.java
 * A class representing use of method flush() of FilterOutputStream 
class in java.io Package.
 * @Version  10-June-2008
 @author   Rose India Team
 */
import java.io.*;

public class FlushFilterOutputStream extends FilterOutputStream {

    FlushFilterOutputStream(FileOutputStream objFin) {
        super(objFin);
    }

    public static void main(String[] argsthrows IOException {

        byte[] byteArray1 = new byte[]{'M''A''H''E''N''D''R''A'};

        //Create a file with a specified name and file extension.
        File file = new File("mahendra.txt");

        // Create a output stream and write specified bytes in file.
        FileOutputStream objFStream = new FileOutputStream(file);
        FlushFilterOutputStream obj = new FlushFilterOutputStream(objFStream);
        obj.write(byteArray1);

        // create a file input stream to read content of the file.
        InputStream in = new FileInputStream(file);
        System.out.print("Content of the file : ");
        for (int c = in.read(); c != -1; c = in.read()) {
            System.out.print((charc);
        }
        // flush method call.
        System.out.print("\nflush method is called for this output stream.");
        obj.flush();
    }
}

output of the program.
 Content of the file : MAHENDRA
flush method is called for this output stream.

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US