Java CharBuffer read()方法及示例

Java CharBuffer read()方法及示例

java.nio.CharBuffer类read() 方法用于向指定的字符缓冲区读取字符。缓冲区作为一个字符库被原封不动地使用:唯一的变化是放操作的结果。不会对缓冲区进行翻转或倒退。

语法

public int read(CharBuffer target)

参数: 该方法接收要读入字符的缓冲区。

返回值: 该方法返回添加到缓冲区的字符数,如果这个字符源已经到了尽头,则返回-1。

异常: 该方法会抛出以下异常: – IOException

  • IOException – 如果发生I/O错误
  • NullPointerException – 如果目标为空
  • ReadOnlyBufferException – 如果目标是一个只读缓冲区。

下面是一些例子来说明read()方法。

例子 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

例2: 对于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

例3: 对于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

参考资料: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#read-java.nio.CharBuffer-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 参考指南