例子
以下示例显示java.io.FilterWriter.flush()方法的用法。
package com.jc2182;
import java.io.FilterWriter;
import java.io.StringWriter;
import java.io.Writer;
public class FilterWriterDemo {
public static void main(String[] args) throws Exception {
FilterWriter fw = null;
Writer w = null;
String s = null;
try {
// create new reader
w = new StringWriter(6);
// filter writer
fw = new FilterWriter(w) {
};
// write to filter writer
fw.write(65);
// flushes the content
fw.flush();
System.out.println("flush() invoked");
System.out.println("flushes content to the writer");
// get the string
s = w.toString();
// print
System.out.print("String: "+s);
} catch(Exception e) {
// if any I/O error occurs
e.printStackTrace();
} finally {
// releases system resources associated with this stream
if(w!=null)
w.close();
if(fw!=null)
fw.close();
}
}
}
让我们编译并运行以上程序,这将产生以下结果-
flush() invoked
flushes content to the writer
String: A