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

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

Syntax:

void java.io.FilterOutputStream.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 write method of FilterOutputStreamcalls the write method of one argument on each byte to output.

Note that this method does not call the write method of its underlying output stream with the same arguments. Subclasses of FilterOutputStream should provide a more efficient implementation of this method.

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.

Throws:

1. NullPointerException - If b is null.

2. IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off.

3. IOException - if an I/O error occurs.

Approach 1: When no exception

Java

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;

public class FilterOutputStreamwrite3 {
    public static void main(String[] args) throws IOException {
        File data = new File("D:\\hello.txt");
        FileOutputStream file = new FileOutputStream(data);
        FilterOutputStream filter = new FilterOutputStream(file);

        byte b[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'j', 'a', 'v', 'a' };
        int off = 0, len = 5;
        filter.write(b, off, len);

        System.out.println("Successfully write into the filter output stream");
        filter.close();
    }
}

Output:

Successfully write into the filter output stream


hello.txt file contains the below data

hello


Approach 2: NullPointerException 

Java

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;

public class FilterOutputStreamwrite3 {
    public static void main(String[] args) throws IOException {
        File data = new File("D:\\hello.txt");
        FileOutputStream file = new FileOutputStream(data);
        FilterOutputStream filter = new FilterOutputStream(file);

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

        System.out.println("Successfully write into the filter output stream");
        filter.close();
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "b" is null at java.base/java.io.FilterOutputStream.write(FilterOutputStream.java:133)



Approach 3: IndexOutOfBoundsException 

Java

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;

public class FilterOutputStreamwrite3 {
    public static void main(String[] args) throws IOException {
        File data = new File("D:\\hello.txt");
        FileOutputStream file = new FileOutputStream(data);
        FilterOutputStream filter = new FilterOutputStream(file);

        byte b[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'j', 'a', 'v', 'a' };
        int off = 0, len = 100;
        filter.write(b, off, len);

        System.out.println("Successfully write into the filter output stream");
        filter.close();
    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.FilterOutputStream.write(FilterOutputStream.java:134)


Approach 4: IOException 

Java

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;

public class FilterOutputStreamwrite3 {
    public static void main(String[] args) throws IOException {
        File data = new File("D:\\hello.txt");
        FileOutputStream file = new FileOutputStream(data);
        FilterOutputStream filter = new FilterOutputStream(file);

        byte b[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'j', 'a', 'v', 'a' };
        int off = 0, len = 5;
       
        filter.close();
        filter.write(b, off, len);

        System.out.println("Successfully write into the filter output stream");
        filter.close();
    }
}

Output:

Exception in thread "main" java.io.IOException: Stream Closed at java.base/java.io.FileOutputStream.write(Native Method) at java.base/java.io.FileOutputStream.write(FileOutputStream.java:311) at java.base/java.io.FilterOutputStream.write(FilterOutputStream.java:87) at java.base/java.io.FilterOutputStream.write(FilterOutputStream.java:137)


Some other methods of FilterOutputStream

close()This method closes this output stream and releases any system resources associated with the stream.

flush()This method flushes this output stream and forces any buffered output bytes to be written out to the stream.

write(byte[])This method writes b.length bytes to this output stream.

write(int)This method writes the specified byte to this output stream.


No comments:

Post a Comment