例子
以下示例显示java.io.PrintWriter.close()方法的用法。
package com.jc2182;
import java.io.*;
public class PrintWriterDemo {
public static void main(String[] args) {
String s = "Hello World ";
try {
// create a new stream at system
PrintWriter pw = new PrintWriter(System.out);
// append the sequence
pw.append(s);
// flush the writer
pw.flush();
// print another string
pw.println("This is an example");
// flush the writer again
pw.flush();
// close the writer
pw.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行以上程序,这将产生以下结果-
Hello World This is an example