OutputStream write(int) in Java

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

Syntax:

void java.io.OutputStream.write(int b) throws IOException

This method takes one argument. This method writes the specified byte to this output stream.

The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of argument b. The 24 high-order bits of b are ignored.

Parameters: One parameter is required for this method.

b: the byte.

Returns: NA

Throws:

IOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.

Approach

Java

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

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

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

        int b = 'a';
        outputStream.write(b);

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

Output:

Successfully writes


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