FilterWriter flush() in Java

flush(): This method is available in the java.io.FilterWriter class of Java.

Syntax:

void java.io.FilterWriter.flush() throws IOException

This method flushes the stream.

Parameters: NA

Returns: NA

Throws:

IOException - If an I/O error occurs

Approach

Java

import java.io.FilterWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

public class FilterWriterflush {
    public static void main(String[] args) throws IOException {

        Writer writer = new StringWriter();
        FilterWriter filterWriter = new FilterWriter(writer) {
        };

        try {
            filterWriter.flush();
            System.out.println("Successfully flushed the filter writer");
        } catch (IOException e) {
            System.out.println("IOException occures");
        }
        filterWriter.close();
    }
}

Output:

Successfully flushed the filter writer


Some other methods of FilterWriter.

close()This method closes the stream, flushing it first. 

write(int)This method writes a single character.

write(char[], int, int)This method writes a portion of an array of characters.

write(String, int, int)This method writes a portion of a string.

No comments:

Post a Comment