void close()
描述 (Description)
java.io.OutputStreamWriter.close()方法关闭流,首先刷新它。 关闭流后,进一步的write()或flush()调用将导致抛出IOException。 关闭先前关闭的流无效。
声明 (Declaration)
以下是java.io.OutputStreamWriter.close()方法的声明。
public void close()
参数 (Parameters)
NA
返回值 (Return Value)
此方法不返回值。
异常 (Exception)
IOException - 如果发生I/O错误。
例子 (Example)
以下示例显示了java.io.OutputStreamWriter.close()方法的用法。
package com.iowiki;
import java.io.*;
public class OutputStreamWriterDemo {
public static void main(String[] args) {
try {
// create a new OutputStreamWriter
OutputStream os = new FileOutputStream("test.txt");
OutputStreamWriter writer = new OutputStreamWriter(os);
// create a new FileInputStream to read what we write
FileInputStream in = new FileInputStream("test.txt");
// write something in the file
writer.write(70);
// flush the stream
writer.flush();
// read what we write
System.out.println("" + (char) in.read());
// close the stream
System.out.println("Closing Stream...");
writer.close();
System.out.println("Stream closed.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
F
Closing Stream...
Stream closed.