📜  Java I/O-FilterWriter

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

Java FilterWriter

Java FilterWriter类是一个抽象类,用于编写过滤的字符流。

FilterWriter的子类应覆盖其某些方法,并且它可能还提供其他方法和字段。

领域

Modifier Type Field Description
protected Writer out The underlying character-output stream.

建设者

Modifier Constructor Description
protected FilterWriter(Writer out) It creates InputStream class Object

方法

Modifier and Type Method Description
void close() It closes the stream, flushing it first.
void flush() It flushes the stream.
void write(char[] cbuf, int off, int len) It writes a portion of an array of characters.
void write(int c) It writes a single character.
void write(String str, int off, int len) It writes a portion of a string.

FilterWriter示例

import java.io.*;
class CustomFilterWriter extends FilterWriter {
CustomFilterWriter(Writer out) {
super(out);
}
public void write(String str) throws IOException {
super.write(str.toLowerCase());
}
}
public class FilterWriterExample {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("Record.txt"); 
CustomFilterWriter filterWriter = new CustomFilterWriter(fw);
filterWriter.write("I LOVE MY COUNTRY");
filterWriter.close();
FileReader fr = new FileReader("record.txt");
BufferedReader bufferedReader = new BufferedReader(fr);
int k;
while ((k = bufferedReader.read()) != -1) {
System.out.print((char) k);
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

输出:

i love my country

如果当前工作目录中不包含该文件,则在运行当前程序时,将创建一个新文件,CustomFileWriter会将小写的文本“ I LOVE MY COUNTRY”写入该文件。