OutputStream flush() in Java

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

Syntax:

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

This method flushes this output stream and forces any buffered output bytes to be written out.

The general contract of flush is that calling it indicates that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be reported to their intended destination.

Parameters: NA

Returns: NA

Throws:

IOException - if an I/O error occurs.

Approach

Java

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

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

        OutputStream outputStream =
new FileOutputStream("D:\\hello.txt");

        outputStream.flush();

        System.out.println("Successfully flushed");
        outputStream.close();
    }
}

Output:

Successfully flushed


Some other methods of OutputStream

close()This method closes this output stream and releases any system resources associated with this stream. 

flush()This method flushes this output stream and forces any buffered output bytes to be written out.

OutputStream.nullOutputStream()This method returns a new OutputStream which discards all bytes.

write(byte[])This method writes b.length bytes from the specified byte array to this output stream.

write(int)This method writes the specified byte to this output stream

write(byte[], int, int)This method writes len bytes from the specified byte array starting at offset off to this output stream.

No comments:

Post a Comment