例子
以下示例显示java.io.FileOutputStream.close()方法的用法。
package com.jc2182;
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 ,其内容如下。该文件将用作示例程序的输入-
让我们编译并运行以上程序,这将产生以下结果-
IOException: File output stream is closed