writeBytes(byte[]): This method is available in the java.io.ByteArrayOutputStream class of Java.
Syntax:
void java.io.ByteArrayOutputStream.writeBytes(byte[] b)
This method takes one argument. This method writes the complete contents of the specified byte array to this ByteArrayOutputStream.
Parameters: One parameter is required for this method.
b: the data.
Returns: NA
Throws:
NullPointerException - if b is null.
Approach 1: When no exception
Java
import java.io.ByteArrayOutputStream;import java.io.IOException;public class ByteArrayOutputStreamwrite3 {public static void main(String[] args) throws IOException {ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream();byte b[] = { '1', '2', '3', '4' };byteArrayOutputStream.writeBytes(b);System.out.println(byteArrayOutputStream.toString());byteArrayOutputStream.close();}}
Output:
1234
Approach 2: NullPointerException
Java
import java.io.ByteArrayOutputStream;import java.io.IOException;public class ByteArrayOutputStreamwrite3 {public static void main(String[] args) throws IOException {ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream();byte b[] = null;byteArrayOutputStream.writeBytes(b);System.out.println(byteArrayOutputStream.toString());byteArrayOutputStream.close();}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "b" is null at java.base/java.io.ByteArrayOutputStream.writeBytes(ByteArrayOutputStream.java:148)
No comments:
Post a Comment