Java CharArrayReader.read()方法範例

2019-10-16 22:15:30

Java CharArrayReader.read()方法範例

CharArrayReaderCharArrayReader.read()方法的語法如下。

public int read()  throws IOException

範例

在下面的程式碼中展示了如何使用CharArrayReader.read()方法。

import java.io.CharArrayReader;

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

    char[] ch = { 'H', 'E', 'L', 'L', 'O' };


    CharArrayReader car = new CharArrayReader(ch);

    int value = 0;

    // read till the end of the file
    while ((value = car.read()) != -1) {

      char c = (char) value;

      // print the character // At: WWW.Yi i Ba I  .COM 
      System.out.print(c + " : ");

      // print the integer
      System.out.println(value);
    }

  }
}

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

H : 72
E : 69
L : 76
L : 76
O : 79