例子
以下示例显示java.io.Writer.write()方法的用法。
package com.jc2182;
import java.io.*;
public class WriterDemo {
public static void main(String[] args) {
String str = "Hello world!";
// create a new writer
Writer writer = new PrintWriter(System.out);
try {
// write a string
writer.write(str);
// flush the writer
writer.flush();
// change line and write another string
writer.write("\nThis is an example");
// flush the stream again
writer.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行以上程序,这将产生以下结果-
Hello world!
This is an example