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

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

Syntax:

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

This method takes three arguments. This method writes a sub array of bytes.

Parameters: Three parameters are required for this method.

buf: the data to be written.

off: the start offset in the data.

len: the number of bytes that are written.

Returns: NA

Throws:

1. IOException - If an I/O error has occurred.

2. NullPointerException - If buf is null.

3. IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than buf.length - off.

Approach 1: When no exception

Java

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

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

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

        ObjectOutputStream objStream = new ObjectOutputStream(fileStream);

        byte buf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
        int off = 0, len = 3;
        objStream.write(buf, off, len);
        System.out.println("Successfully writes to the stream");

        objStream.close();
    }
}

Output:

Successfully writes to the stream


Approach 2: NullPointerException 

Java

package com.ObjectOutputStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

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

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

        ObjectOutputStream objStream = new ObjectOutputStream(fileStream);

        byte buf[] = null;
        int off = 0, len = 3;
        objStream.write(buf, off, len);
        System.out.println("Successfully writes to the stream");

        objStream.close();
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.io.ObjectOutputStream.write(ObjectOutputStream.java:710) at com.ObjectOutputStream.ObjectOutputStreamwrite3.main(ObjectOutputStreamwrite3.java:16)


Approach 3: IndexOutOfBoundsException 

Java

package com.ObjectOutputStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

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

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

        ObjectOutputStream objStream = new ObjectOutputStream(fileStream);

        byte buf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
        int off = 0, len = 13;
        objStream.write(buf, off, len);
        System.out.println("Successfully writes to the stream");

        objStream.close();
    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.ObjectOutputStream.write(ObjectOutputStream.java:714) at com.ObjectOutputStream.ObjectOutputStreamwrite3.main(ObjectOutputStreamwrite3.java:16)


Some more methods of ObjectOutputStream

close()This method must be called to release any resources associated with the stream.

defaultWriteObject()This method writes the non-static and non-transient fields of the current class to this stream.

flush()This method flushes the stream.

putFields()This method retrieves the object used to buffer persistent fields to be written to the stream.

reset()The state is reset to be the same as a new ObjectOutputStream.

useProtocolVersion(int)This method specifies the stream protocol version to use when writing the stream.

write(byte[])This method writes an array of bytes. 

write(int)This method writes a byte.

writeBoolean(boolean)This method writes a boolean.

writeByte(int)This method writes an 8-bit byte.

writeBytes(String)This method writes a String as a sequence of bytes.

writeChar(int)This method writes a 16-bit char.

writeChars(String)This method writes a String as a sequence of chars.

writeDouble(double)This method writes a 64-bit double.

writeFields()This method writes the buffered fields to the stream.

writeFloat(float)This method writes a 32-bit float.

writeInt(int)This method writes a 32-bit int.

writeLong(long)This method writes a 64-bit long.

writeObject(Object)This method writes the specified object to the ObjectOutputStream.

writeShort(int)This method writes a 16-bit short.

writeUnshared(Object)This method writes an "unshared" object to the ObjectOutputStream.

writeUTF(String)This method is primitive data are written of this String in modified UTF-8 format.

No comments:

Post a Comment