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:
import java.nio.*;
import java.util.*;
import java.io.IOException;
public class GFG {
public static void main(String[] args)
{
try {
char [] cb1 = { 'x' , 'y' , 'z' };
char [] cb2 = { 'a' , 'b' , 'c' , 'd' , 'e' };
CharBuffer charBuffer1
= CharBuffer.wrap(cb1);
CharBuffer charBuffer2
= CharBuffer.wrap(cb2);
System.out.println( "CharBuffer Before operation is: "
+ Arrays.toString(
charBuffer1.array())
+ "\nTarget Charbuffer: "
+ Arrays.toString(
charBuffer2.array()));
int value
= charBuffer1
.read(charBuffer2);
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
import java.nio.*;
import java.util.*;
import java.io.IOException;
public class GFG {
public static void main(String[] args)
{
try {
char [] cb1 = { 'x' , 'y' , 'z' };
CharBuffer charBuffer1
= CharBuffer.wrap(cb1);
System.out.println( "CharBuffer Before operation is: "
+ Arrays.toString(
charBuffer1.array()));
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
import java.nio.*;
import java.util.*;
import java.io.IOException;
public class GFG {
public static void main(String[] args)
{
try {
char [] cb1 = { 'x' , 'y' , 'z' };
char [] cb2 = { 'a' , 'b' , 'c' , 'd' , 'e' };
CharBuffer charBuffer1
= CharBuffer.wrap(cb1);
CharBuffer charBuffer2
= CharBuffer.wrap(cb2);
System.out.println( "CharBuffer Before operation is: "
+ Arrays.toString(
charBuffer1.array())
+ "\nTarget Charbuffer: "
+ Arrays.toString(
charBuffer2.array()));
CharBuffer readonlybuff
= charBuffer2.asReadOnlyBuffer();
int value = charBuffer1.read(readonlybuff);
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-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...