Java.io.PushbackReader.read() 方法



描述

java.io.PushbackReader.read(char[] cbuf,int off,int len) 方法將字元讀取到陣列的一部分。

宣告

以下是java.io.PushbackReader.read() 方法的宣告。

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

引數

  • cbuf − 目標緩衝區。

  • off − 開始寫入字元的偏移量。

  • len − 要讀取的字元的最大數量。

返回值

此方法返回讀取的字元數,如果已到達流的結尾,則返回 -1。

異常

IOException − 如果發生 I/O 錯誤

示例

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

package com.tutorialspoint;

import java.io.*;

public class PushbackReaderDemo {
   public static void main(String[] args) {
      String s = "Hello World";

      // create a new StringReader
      StringReader sr = new StringReader(s);

      // create a new PushBack reader based on our string reader
      PushbackReader pr = new PushbackReader(sr, 20);

      // create a char array to read chars into
      char cbuf[] = new char[5];

      try {
         // read characters into an array.
         System.out.println("" + pr.read(cbuf));

         // print cbuf
         System.out.println(cbuf);

         // Close the stream 
         pr.close();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

讓我們編譯並執行上面的程式,這將產生以下結果:

5
Hello
java_io_pushbackreader.htm
廣告
© . All rights reserved.