🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
The AutoCloseable interface in Java provides a mechanism for closing resources automatically when they are no longer needed. It is commonly used in try-with-resources statements to manage resource cleanup.
Table of Contents
- What is
AutoCloseable? - Key Methods
- Implementations
- Examples of
AutoCloseable - Conclusion
1. What is AutoCloseable?
AutoCloseable is an interface that allows objects to be closed automatically. Classes that implement AutoCloseable can release resources like file handles or database connections when they are no longer in use.
2. Key Methods
close(): Closes the resource, releasing any system resources it holds. This method is called automatically at the end of a try-with-resources block.
3. Implementations
Common classes that implement AutoCloseable include:
InputStreamand its subclasses, such asFileInputStreamOutputStreamand its subclasses, such asFileOutputStreamReaderandWriterclassesConnectionin JDBC
4. Examples of AutoCloseable
Example 1: Using AutoCloseable with FileInputStream
This example demonstrates how to use AutoCloseable in a try-with-resources block with FileInputStream.
import java.io.FileInputStream;
import java.io.IOException;
public class AutoCloseableExample {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("example.txt")) {
int content;
while ((content = fis.read()) != -1) {
System.out.print((char) content);
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Example 2: Creating a Custom AutoCloseable Class
Here, we create a custom class that implements AutoCloseable and use it in a try-with-resources block.
public class CustomResource implements AutoCloseable {
public void useResource() {
System.out.println("Using resource...");
}
@Override
public void close() {
System.out.println("Resource closed.");
}
public static void main(String[] args) {
try (CustomResource resource = new CustomResource()) {
resource.useResource();
}
}
}
Output:
Using resource...
Resource closed.
Conclusion
The AutoCloseable interface in Java is a convenient way to manage resource cleanup automatically. It simplifies resource management and reduces the likelihood of resource leaks by ensuring that resources are properly closed. Using AutoCloseable in try-with-resources blocks leads to cleaner and more maintainable code.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment