🎓 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
The close() method in Java, part of the java.util.Scanner class, is used to close the Scanner object and release any resources associated with it.
Table of Contents
- Introduction
close()Method Syntax- Understanding
close() - Examples
- Basic Usage
- Handling Exceptions
- Real-World Use Case
- Conclusion
Introduction
The close() method closes the Scanner and releases any resources associated with it. It is important to close the Scanner when it is no longer needed to free up system resources.
close() Method Syntax
The syntax for the close() method is as follows:
public void close()
Parameters:
- This method does not take any parameters.
Returns:
- This method does not return a value.
Understanding close()
The close() method should be called when you are done using the Scanner to prevent resource leaks. After calling close(), the Scanner object cannot be used for further input.
Examples
Basic Usage
To demonstrate the basic usage of close(), we will create a Scanner object, read some input, and then close the Scanner.
Example
import java.util.Scanner;
public class ScannerCloseExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
// Close the scanner
scanner.close();
}
}
Handling Exceptions
This example shows how to handle exceptions when using the close() method.
Example
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ScannerCloseWithExceptionHandling {
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(new File("example.txt"));
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
} finally {
if (scanner != null) {
scanner.close();
}
}
}
}
Real-World Use Case
Reading from a File
In real-world applications, the Scanner is often used to read input from files. It is crucial to close the Scanner after reading to ensure that file handles and other system resources are properly released.
Example
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FileReadingExample {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(new File("data.txt"))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
}
}
}
In this example, the Scanner is used within a try-with-resources block, which automatically closes the Scanner at the end of the block, ensuring that resources are released.
Conclusion
The Scanner.close() method is essential for releasing resources associated with the Scanner object. Always ensure that you close the Scanner when it is no longer needed to prevent resource leaks and ensure efficient resource management.
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