Java FilterReader.read(char[] cbuf, int off, int len)方法範例

2019-10-16 22:13:36

FilterReader.read(char[] cbuf, int off, int len)方法範例

FilterReader.read(char[] cbuf, int off, int len)具有以下語法。

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

範例

在下面的程式碼中展示了如何使用FilterReader.read(char[] cbuf, int off, int len)方法。

import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;

public class Main {
  public static void main(String[] args) throws Exception {

    char[] cbuf = new char[6];
    int i = 0;// at W w  W. y Ii b  Ai .c O M

    Reader r = new StringReader("ABCDEF");

    FilterReader fr = new FilterReader(r) {
    };

    // read data of len 4, to the buffer
    i = fr.read(cbuf, 2, 4);
    System.out.println("No. of characters read: " + i);

    // read till the end of the char buffer
    for (char c : cbuf) {
      // checks for the empty character in buffer
      if ((byte) c == 0)
        c = '-';

      System.out.print(c);
    }

  }
}

上面的程式碼生成以下結果。

No. of characters read: 4
--ABCD