OutputStream write(byte[]) in Java

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

Syntax:

void java.io.OutputStream.write(byte[] b) throws IOException

This method takes one argument. This method writes b.length bytes from the specified byte array to this output stream.

The general contract for write(b)is that it should have exactly the same effect as the call write(b, 0, b.length).

Parameters: One parameter is required for this method.

b: the data.

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

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

        byte b[] = { 'a', 'b', 'c', 'd' };
        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