Java.io.PrintWriter.close() 方法
描述
java.io.PrintWriter.close() 方法关闭流并释放与其关联的所有系统资源。
声明
以下是 java.io.PrintWriter.close() 方法的声明。
public void close()
参数
NA
返回值
此方法不返回值。
异常
NA
示例
下面的例子展示了 java.io.PrintWriter.close() 方法的使用。
package com.tutorialspoint;
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

