CharBuffer read() methods in Java with Examples
Last Updated :
28 Jul, 2019
The
read() method of
java.nio.CharBuffer Class is used to read characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation. No flipping or rewinding of the buffer is performed.
Syntax:
public int read(CharBuffer target)
Parameter: This method takes the buffer to read characters into.
Return Value: This method returns the number of characters added to the buffer, or -1 if this source of characters is at its end.
Exception: This method throws following exception:-
- IOException - if an I/O error occurs
- NullPointerException - if target is null
- ReadOnlyBufferException - if target is a read only buffer
Below are the examples to illustrate the read() method:
Examples 1:
Java
// Java program to demonstrate
// read() method
import java.nio.*;
import java.util.*;
import java.io.IOException;
public class GFG {
public static void main(String[] args)
{
try {
// Declare and initialize the char array
char[] cb1 = { 'x', 'y', 'z' };
char[] cb2 = { 'a', 'b', 'c', 'd', 'e' };
// wrap the char array into CharBuffer
// using wrap() method
CharBuffer charBuffer1
= CharBuffer.wrap(cb1);
// wrap the char array into CharBuffer
// using wrap() method
CharBuffer charBuffer2
= CharBuffer.wrap(cb2);
// print the byte buffer
System.out.println("CharBuffer Before operation is: "
+ Arrays.toString(
charBuffer1.array())
+ "\nTarget Charbuffer: "
+ Arrays.toString(
charBuffer2.array()));
// Get the value of the number of Character
// read from the charBuffer
// using read() method
int value
= charBuffer1
.read(charBuffer2);
// print the byte buffer
System.out.println("\nCharBuffer After operation is: "
+ Arrays.toString(
charBuffer1.array())
+ "\nTarget Charbuffer: "
+ Arrays.toString(
charBuffer2.array())
+ "\nno of value changed: "
+ value);
}
catch (IOException e) {
System.out.println("an I/O error occurs");
System.out.println("Exception throws: " + e);
}
catch (NullPointerException e) {
System.out.println("target charbuffer is null");
System.out.println("Exception throws: " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("target is a read only buffer");
System.out.println("Exception throws: " + e);
}
}
}
Output:
CharBuffer Before operation is: [x, y, z]
Target Charbuffer: [a, b, c, d, e]
CharBuffer After operation is: [x, y, z]
Target Charbuffer: [x, y, z, d, e]
no of value changed: 3
Examples 2: For NullPointerException
Java
// Java program to demonstrate
// read() method
import java.nio.*;
import java.util.*;
import java.io.IOException;
public class GFG {
public static void main(String[] args)
{
try {
// Declare and initialize the char array
char[] cb1 = { 'x', 'y', 'z' };
// wrap the char array into CharBuffer
// using wrap() method
CharBuffer charBuffer1
= CharBuffer.wrap(cb1);
// print the byte buffer
System.out.println("CharBuffer Before operation is: "
+ Arrays.toString(
charBuffer1.array()));
// Get the value of number of Character
// read from the charBuffer
// using read() method
int value = charBuffer1.read(null);
}
catch (IOException e) {
System.out.println("\nan I/O error occurs");
System.out.println("Exception throws: " + e);
}
catch (NullPointerException e) {
System.out.println("\ntarget charbuffer is null");
System.out.println("Exception throws: " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("\ntarget is a read only buffer");
System.out.println("Exception throws: " + e);
}
}
}
Output:
CharBuffer Before operation is: [x, y, z]
target charbuffer is null
Exception throws: java.lang.NullPointerException
Examples 3: For ReadOnlyBufferException
Java
// Java program to demonstrate
// read() method
import java.nio.*;
import java.util.*;
import java.io.IOException;
public class GFG {
public static void main(String[] args)
{
try {
// Declare and initialize the char array
char[] cb1 = { 'x', 'y', 'z' };
char[] cb2 = { 'a', 'b', 'c', 'd', 'e' };
// wrap the char array into CharBuffer
// using wrap() method
CharBuffer charBuffer1
= CharBuffer.wrap(cb1);
// wrap the char array into CharBuffer
// using wrap() method
CharBuffer charBuffer2
= CharBuffer.wrap(cb2);
// print the byte buffer
System.out.println("CharBuffer Before operation is: "
+ Arrays.toString(
charBuffer1.array())
+ "\nTarget Charbuffer: "
+ Arrays.toString(
charBuffer2.array()));
// converting Charbuffer to readonlybuff
CharBuffer readonlybuff
= charBuffer2.asReadOnlyBuffer();
// Get the value of number of Character
// read from the charBuffer
// using read() method
int value = charBuffer1.read(readonlybuff);
// print the byte buffer
System.out.println("\nCharBuffer After operation is: "
+ Arrays.toString(
charBuffer1.array())
+ "\nTarget Charbuffer: "
+ Arrays.toString(
charBuffer2.array())
+ "\nno of value changed: "
+ value);
}
catch (IOException e) {
System.out.println("\nan I/O error occurs");
System.out.println("Exception throws: " + e);
}
catch (NullPointerException e) {
System.out.println("\ntarget charbuffer is null");
System.out.println("Exception throws: " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("\ntarget is a read only buffer");
System.out.println("Exception throws: " + e);
}
}
}
Output:
CharBuffer Before operation is: [x, y, z]
Target Charbuffer: [a, b, c, d, e]
target is a read only buffer
Exception throws: java.nio.ReadOnlyBufferException
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#read-java.nio.CharBuffer-