Javatpoint Logo
Javatpoint Logo

FileInputStream finalize() Method in Java

The FileInputStream class of the Java programming language is used to read data from files in a byte-oriented fashion. It has several data reading methods, including read(), read(byte[]), and read(byte[], int, int). Finalise(), a method that the FileInputStream class inherits from the Object class, is one of its less well-known counterparts. Using examples, we will examine and analyze the finalise() function in FileInputStream in this post.

Understanding the finalize() Method

The finalize() method is a special method in Java that is automatically called by the garbage collector when an object is about to be garbage-collected. It is defined in the Object class and can be overridden by any class to perform specific cleanup tasks before the object is destroyed.

The finalize() method in FileInputStream is called by the garbage collector to release any system resources associated with the file input stream. It is typically used to ensure the file input stream is properly closed and that any operating system resources, such as file descriptors, are freed.

Syntax

The FileInputStream finalise() function has the following syntax:

The finalise() method is marked with the protected access modifier, which restricts access to FileInputStream subclasses or members of the same package. It raises a Throwable exception that enables subclasses to raise errors or exceptions.

Example

Let's look at an instance where we open a FileInputStream to read data from a file and use the finalise() function to make sure the object is properly cleaned away when it isn't required any longer:

Output:

Hi, this content is in an a.txt file

Explanation

We have a class with a readFile() function in this code. We construct a FileInputStream object with the filename "a.txt" within this function. Then, until the end of the file is reached (indicated by the read() function returning -1), we read and print each character from the file using a while loop.

a.txt

Importance of Resource Cleanup

Correctly managing and releasing resources, such as files, network connections, and database connections, is essential when dealing with them. Please accomplish this to avoid resource leaks, which may result in problems like running out of file descriptors or using all available network connections.

Using the close() method offered by resource-specific classes, Java provides a mechanism to relinquish resources expressly. However, it might be simple to forget to use the close() function in some circumstances, which can result in resource leakage. The finalize () function was added to the Object class to solve this as a last-ditch solution for resource cleaning.

Garbage Collection and Finalization

To free up memory that has been used by objects that are no longer referenced by the programme, Java utilises automated garbage collection. The garbage collector designates an item as being inaccessible and releases the memory it resides in. The finalise() function is used to give any cleanup procedures a chance to complete before the object is destroyed.

How FileInputStream's finalise() Operates

When the item is no longer available, the garbage collector calls the finalise() function on FileInputStream. Its goal is to free all system resources connected to the FileInputStream, including file handles and file descriptors.

The InputStream class, which is the superclass of FileInputStream, is where FileInputStream derives its finalise() function from. The close() function is invoked in order to release any system resources retained by the stream thanks to the implementation of finalise() in the InputStream class.

The close() function is called internally by the finalise() method in the case of FileInputStream. It's crucial to remember, though, that depending on finalise() for resource cleaning is not advised for a number of reasons.

Conclusion

An object can be cleaned up with the help of the FileInputStream's finalise() function before being destroyed by the garbage collector. It is not advised to use this technique for resource cleaning in Java. To guarantee effective resource management, utilize the close() function offered by the resource-specific classes and best practices such as the try-with-resources statement.

By explicitly shutting resources, you may prevent resource leaks and guarantee effective and dependable resource consumption in your Java programs.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA