📜  Java I/O-FilterReader

📅  最后修改于: 2020-09-27 07:38:04             🧑  作者: Mango

Java FilterReader

Java FilterReader用于对读取器流执行过滤操作。它是用于读取过滤的字符流的抽象类。

FilterReader提供将所有请求传递到所包含的流的默认方法。 FilterReader的子类应覆盖其某些方法,并且可能还提供其他方法和字段。

领域

Modifier Type Field Description
protected Reader in The underlying character-input stream.

建设者

Modifier Constructor Description
protected FilterReader(Reader in) It creates a new filtered reader.

方法

Modifier and Type Method Description
void close() It closes the stream and releases any system resources associated with it.
void mark(int readAheadLimit) It marks the present position in the stream.
boolean markSupported() It tells whether this stream supports the mark() operation.
boolean ready() It tells whether this stream is ready to be read.
int read() It reads a single character.
int read(char[] cbuf, int off, int len) It reads characters into a portion of an array.
void reset() It resets the stream.
long skip(long n) It skips characters.

在此示例中,我们使用“ javaFile123.txt”文件,其中包含“印度是我的国家”文本。在这里,我们正在转换带有问号’?’的空格。

import java.io.*;
class CustomFilterReader extends FilterReader {
CustomFilterReader(Reader in) {
super(in);
}
public int read() throws IOException {
int x = super.read();
if ((char) x == ' ')
return ((int) '?');
else
return x;
}
}
public class FilterReaderExample {
public static void main(String[] args) {
try  {
Reader reader = new FileReader("javaFile123.txt");
CustomFilterReader fr = new CustomFilterReader(reader);
int i;
while ((i = fr.read()) != -1) {
System.out.print((char) i);
}
            fr.close();
            reader.close();
} catch (Exception e) {
e.getMessage();
}
}
}

输出:

India?is?my?country