天天看点

java printwriter 没有关闭_Java PrintWriter close()方法

Java PrintWriter close()方法

java.io.PrintWriter.close() 方法关闭该流并释放与之关联的所有系统资源。

1 语法

public void close()

2 参数

3 返回值

4 示例

package com.yiidian;

import java.io.*;

public class Demo {

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