Java FilterOutputStream close() Example

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


Syntax is : public void close() throws IOException
This method closes this output stream and releases any system resources associated with the stream. The close method of
FilterOutputStream calls its flush method, and then calls the close method of its underlying output stream.

Here is the code.

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

public class CloseFilterOutputStream extends FilterOutputStream {

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

    public static void main(String[] argsthrows IOException {

        try {
            byte[] byteArray1 = new byte[]{'M','A','H','E','N','D','R','A'};
            byte[] byteArray2 = new byte[]{'S''I''N''G''H'};

            //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);
            CloseFilterOutputStream obj=new CloseFilterOutputStream(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);
            }

            // close method call that closes this output stream.
           System.out.print("\nclose method is called for this output stream.");
            obj.close();
            obj.write(byteArray2);
        catch (Exception ex) {
            System.out.println("\nCan't write more elements bacause filter " 
                    "stream is closed now.");
        }
    }
}

output of the program.
 Content of the file : MAHENDRA
close method is called for this output stream.
Can't write more elements bacause filter stream is closed now.

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

HOME | COPYRIGHT | CONTACT US