OutputStream.nullOutputStream() in Java

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

Syntax:

OutputStream java.io.OutputStream.nullOutputStream()

This method returns a new OutputStream which discards all bytes.

The returned stream is initially open. The stream is closed by calling the close() method. Subsequent calls to close() have no effect.

While the stream is open, the write(int), write(byte[]), and write(byte[],int, int) methods do nothing.

After the stream has been closed, these methods all throw IOException.

Parameters: NA

Returns: an OutputStream that discards all bytes.

Exceptions: NA

Approach

Java

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

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

        OutputStream outputStream =
OutputStream.nullOutputStream();

        System.out.println(outputStream);
        outputStream.close();
    }
}

Output:

java.io.OutputStream$1@26f0a63f


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