OutputStream write(byte[], int, int) in Java

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

Syntax:

void java.io.OutputStream.write(byte[] b, int off, int len) throws IOException

This method takes three arguments. This method writes len bytes from the specified byte array starting at offset off to this output stream.

The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

Parameters: Three parameters are required for this method.

b: the data.

off: the start offset in the data.

len: the number of bytes to write.

Returns: NA

Throws:

1. IOException - if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.

2. NullPointerException- If b is null.

3. IndexOutOfBoundsException- If off is negative, or len is negative, or off+len is greater than the length of the array b.

Approach 1: When no exception

Java

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

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

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

        byte b[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
        int off = 0, len = 3;
        outputStream.write(b, off, len);

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

Output:

Successfully writes


Approach 2: NullPointerException

Java

package com.OutputStream;

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

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

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

        byte b[] = null;
        int off = 0, len = 3;
        outputStream.write(b, off, len);

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

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.io.FileOutputStream.writeBytes(Native Method) at java.base/java.io.FileOutputStream.write(FileOutputStream.java:347) at com.OutputStream.OutputStreamwrite3.main(OutputStreamwrite3.java:14)


Approach 3: IndexOutOfBoundsException

Java

package com.OutputStream;

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

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

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

        byte b[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
        int off = 0, len = 13;
        outputStream.write(b, off, len);

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

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.FileOutputStream.writeBytes(Native Method) at java.base/java.io.FileOutputStream.write(FileOutputStream.java:347) at com.OutputStream.OutputStreamwrite3.main(OutputStreamwrite3.java:14)


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.

1 comment: