FilterReader.read()方法範例
FilterReader.read()
具有以下語法。
public int read() throws IOException
在下面的程式碼中展示了如何使用FilterReader.read()
方法。
import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;
public class Main {// from w wW.yI I B A I .CO m
public static void main(String[] args) throws Exception {
int i = 0;
char c;
// create new reader
Reader r = new StringReader("ABCDEF");
// create new filter reader
FilterReader fr = new FilterReader(r) {
};
// read till the end of the stream
while ((i = fr.read()) != -1) {
// converts integer to character
c = (char) i;
// print
System.out.println("Character read: " + c);
}
}
}
上面的程式碼生成以下結果。
Character read: A
Character read: B
Character read: C
Character read: D
Character read: E
Character read: F