📌  相关文章
📜  Java中的 CharArrayReader read(CharBuffer) 方法及示例

📅  最后修改于: 2022-05-13 01:54:24.892000             🧑  作者: Mango

Java中的 CharArrayReader read(CharBuffer) 方法及示例

Java中CharArrayReader类的read(CharBuffer)方法用于将指定的字符读入 CharBuffer 实例。此方法阻塞流直到:

  • 它从流中获取了一些输入。
  • 发生了一些 IOException
  • 它在读取时已到达流的末尾。

句法:

public int read(CharBuffer charBuffer)

参数:此方法接受一个强制参数charBuffer ,它是要写入 Stream 中的 CharBuffer 实例。

返回值:此方法返回一个整数值,它是从流中读取的字符数。如果没有读取任何字符,则返回 -1。

异常:此方法抛出以下异常:

  • IOException:如果输入输出时发生错误。
  • NullPointerException:如果要填充的 CharBuffer 实例为 null
  • ReadOnlyBufferException:如果要填充的 CharBuffer 实例是只读缓冲区

下面的方法说明了 read(CharBuffer) 方法的工作:

方案一:

// Java program to demonstrate
// CharArrayReader read(CharBuffer) method
  
import java.io.*;
import java.util.*;
import java.nio.CharBuffer;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            char[] str = { 'G', 'e', 'e', 'k', 's',
                           'F', 'o', 'r',
                           'G', 'e', 'e', 'k', 's' };
  
            // Create a CharArrayReader instance
            CharArrayReader reader
                = new CharArrayReader(str);
  
            // Get the CharBuffer instance
            // to be read from the stream
            CharBuffer charBuffer
                = CharBuffer.allocate(5);
  
            // Read the charBuffer
            // to this reader using read() method
            // This will put the str in the stream
            // till it is read by the reader
            reader.read(charBuffer);
  
            // Print the read charBuffer
            System.out.println(charBuffer
                                   .flip()
                                   .toString());
  
            reader.close();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
Geeks

方案二:

// Java program to demonstrate
// CharArrayReader read(CharBuffer) method
  
import java.io.*;
import java.util.*;
import java.nio.CharBuffer;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            char[] str = { 'G', 'e', 'e', 'k', 's',
                           'F', 'o', 'r',
                           'G', 'e', 'e', 'k', 's' };
  
            // Create a CharArrayReader instance
            CharArrayReader reader
                = new CharArrayReader(str);
  
            // Get the CharBuffer instance
            // to be read from the stream
            CharBuffer charBuffer
                = CharBuffer
                      .allocate(13);
  
            // Read the charBuffer
            // to this reader using read() method
            // This will put the str in the stream
            // till it is read by the reader
            reader.read(charBuffer);
  
            // Print the read charBuffer
            System.out.println(charBuffer
                                   .flip()
                                   .toString());
  
            reader.close();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
GeeksForGeeks

参考: https://docs.oracle.com/javase/9/docs/api/ Java/io/Reader.html#read-java.nio.CharBuffer-