El método read() de java.nio.CharBuffer Class se utiliza para leer caracteres en el búfer de caracteres especificado. El búfer se utiliza como depósito de caracteres tal cual: los únicos cambios realizados son los resultados de una operación de colocación. No se realiza ningún volteo o rebobinado del búfer.
Sintaxis:
public int read(CharBuffer target)
Parámetro: este método toma el búfer para leer caracteres.
Valor devuelto: este método devuelve el número de caracteres agregados al búfer, o -1 si esta fuente de caracteres está al final.
Excepción: este método arroja la siguiente excepción: –
- IOException : si se produce un error de E/S
- NullPointerException : si el objetivo es nulo
- ReadOnlyBufferException : si el objetivo es un búfer de solo lectura
A continuación se muestran los ejemplos para ilustrar el método read():
Ejemplos 1:
// 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); } } }
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
Ejemplos 2: para NullPointerException
// 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); } } }
CharBuffer Before operation is: [x, y, z] target charbuffer is null Exception throws: java.lang.NullPointerException
Ejemplos 3: Para ReadOnlyBufferException
// 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); } } }
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
Referencia: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#read-java.nio.CharBuffer-
Publicación traducida automáticamente
Artículo escrito por RohitPrasad3 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA