Java.io.FileOutputStream.close() 方法
描述
java.io.FileOutputStream.close() 关闭此文件输出流并释放与此流关联的所有系统资源。
声明
以下是 java.io.FileOutputStream.close() 方法的声明 −
public void close()
参数
NA
返回值
该方法不返回任何值。
异常
NA
示例
下面的例子展示了 java.io.FileOutputStream.close() 方法的使用。
package com.tutorialspoint;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = null;
try {
// create new file output stream
fos = new FileOutputStream("C://text.txt");
// close stream
fos.close();
// try to write into underlying stream
fos.write(65);
fos.flush();
fos.close();
} catch(Exception ex) {
// if any error occurs
System.out.print("IOException: File output stream is closed");
} finally {
// releases all system resources from the streams
if(fos!=null)
fos.close();
}
}
}
假设我们有一个文本文件c:/test.txt,其内容如下。 该文件将用作我们示例程序的输入 −
ABCDEF
让我们编译并运行上面的程序,这将产生下面的结果 −
IOException: File output stream is closed

