Java.io.FileInputStream.finalize() Method
Advertisements
Description
The java.io.FileInputStream.finalize() method ensures that the close method of this file input stream is called when there are no more references to it.
Declaration
Following is the declaration for java.io.FileInputStream.finalize() method:
protected void finalize()
Parameters
NA
Return Value
This method does not return any value.
Exception
IOException -- if an I/O error occurs..
Example
The following example shows the usage of java.io.FileInputStream.finalize() method.
package com.tutorialspoint;
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamAvailable extends FileInputStream {
public FileInputStreamAvailable() throws Exception {
super("C://test.txt");
}
public static void main(String[] args) throws IOException {
FileInputStream fis = null;
int i;
char c;
try{
// create new File input stream
FileInputStreamAvailable fisa = new FileInputStreamAvailable();
// read byte from file input stream
i=fisa.read();
// converts int to char
c=(char)i;
// prints character
System.out.println(c);
// finalize method invoked
fisa.finalize();
// method revoked after finalize metod
i=fisa.read();
c=(char)i;
System.out.println(c);
}catch(Exception ex){
// if any error occurs
System.out.print("Error: read() invoked after finalize()");
}finally{
// releases all system resources from the streams
if(fis!=null)
fis.close();
}
}
}
Assuming we have a text file c:/test.txt, which has the following content. This file will be used as an input for our example program:
ABCDE
Let us compile and run the above program, this will produce the following result:
A Error: read() invoked after finalize()