Java.io.OutputStream.close() 方法
描述
java.io.OutputStream.close() 方法关闭此输出流并释放与此流关联的所有系统资源。 close 的一般约定是它关闭输出流。 关闭的流无法执行输出操作,也无法重新打开。 OutputStream 的 close 方法什么也不做。
声明
以下是 java.io.OutputStream.close() 方法的声明。
public void close()
参数
NA
返回值
此方法不返回值。
异常
IOException − 如果发生 I/O 错误。
示例
下面的例子展示了 java.io.OutputStream.close() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class OutputStreamDemo {
public static void main(String[] args) {
try {
// create a new output stream
OutputStream os = new FileOutputStream("test.txt");
// craete a new input stream
InputStream is = new FileInputStream("test.txt");
// write something
os.write('A');
// flush the stream
os.flush();
// close the stream but it does nothing
os.close();
// read what we wrote
System.out.println("" + (char) is.read());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
A