目录

int read(char[] cbuf, int off, int len)

描述 (Description)

java.io.StringReader.read(char[] cbuf,int off,int len)方法将字符读入数组的一部分。

声明 (Declaration)

以下是java.io.StringReader.read()方法的声明。

public int read(char[] cbuf,int off,int len)

参数 (Parameters)

  • cbuf - 目标缓冲区。

  • off - 开始写字符的偏移量。

  • len - 要读取的最大字符数。

返回值 (Return Value)

此方法返回读取的字符数,如果已到达流的末尾,则返回-1。

异常 (Exception)

IOException - 如果发生I/O错误。

例子 (Example)

以下示例显示了java.io.StringReader.read()方法的用法。

package com.iowiki;
import java.io.*;
public class StringReaderDemo {
   public static void main(String[] args) {
      String s = "Hello world";
      // create a StringReader
      StringReader reader = new StringReader(s);
      // create a char array to read chars into
      char cbuf[] = new char[5];
      try {
         // read characters into a portion of an array.
         reader.read(cbuf, 0, 5);
         // print cbuf
         System.out.println(cbuf);
         // Close the stream 
         reader.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

Hello
↑回到顶部↑
WIKI教程 @2018