Java.io.CharArrayReader.read() 方法
描述
java.io.CharArrayReader.read() 方法读取单个字符。 如果流结束,则返回 -1。
声明
以下是 java.io.CharArrayReader.read() 方法的声明 −
public int read()
参数
NA
返回值
该方法以 0 到 65535 范围内的整数形式返回读取的字符。如果流已到达末尾,则 read() 返回 -1。
异常
IOException − 如果发生任何 I/O 错误。
示例
下面的例子展示了 java.io.CharArrayReader.read() 方法的使用。
package com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) { CharArrayReader car = null;
char[] ch = {'H', 'E', 'L', 'L', 'O'};
try {
// create new character array reader
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
System.out.print(c+" : ");
// print the integer
System.out.println(value);
}
} catch(IOException e) {
// if I/O error occurs
e.printStackTrace();
} finally {
// releases any system resources associated with the stream
if(car!=null)
car.close();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
H : 72 E : 69 L : 76 L : 76 O : 79

