Java.io.FileOutputStream.finalize() 方法
描述
java.io.FileOutputStream.finalize() 方法清理与文件的连接,并确保在不再引用此流时调用此文件输出流的 close 方法。< /p>
声明
以下是 java.io.FileOutputStream.finalize() 方法的声明 −
protected void finalize()
参数
NA
返回值
此方法不返回任何值。
异常
IOException − 如果发生 I/O 错误。
示例
下面的例子展示了 java.io.FileOutputStream.finalize() 方法的使用。
package com.tutorialspoint;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamAvailable extends FileOutputStream {
public FileOutputStreamAvailable() throws Exception {
super("C://test.txt");
}
public static void main(String[] args) throws IOException {
FileOutputStreamAvailable fosa = null;
try {
// create new File input stream
fosa = new FileOutputStreamAvailable();
// read byte from file input stream
fosa.finalize();
// converts int to char
System.out.println("Stream is closed successfully.");
} catch(Exception ex) {
// if any error occurs
ex.printStackTrace();
} finally {
// releases all system resources from the streams
if(fos!=null)
fos.close();
}
}
}
假设我们有一个文本文件c:/test.txt,其内容如下。 该文件将用作我们示例程序的输入 −
ABCDE
让我们编译并运行上面的程序,这将产生下面的结果 −
Stream is closed successfully.

