Java Scanner close() Method

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

  1. Introduction
  2. close() Method Syntax
  3. Understanding close()
  4. Examples
    • Basic Usage
    • Handling Exceptions
  5. Real-World Use Case
  6. 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.

Comments