OutputStream close() in Java

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

Syntax:

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

This method closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

The close method of OutputStream does nothing.

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 OutputStreamclose {
    public static void main(String[] args) throws IOException {

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

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

Output:

Successfully closed


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