Close a Scanner by calling the close() method

To close a Scanner object in Java, call the close() method on the Scanner instance. This tells Java to release the resource — usually a file or input stream — that the Scanner was reading from. Once closed, you cannot read from that Scanner again.

The basic syntax is straightforward: if your Scanner is named scan, you write scan.close(); on its own line. This works whether the Scanner is reading from a file, standard input, or any other input source.

Key Takeaways

  • Call close() on your Scanner object to release the file or input stream it was using.
  • Always close a Scanner that reads from a file; closing a Scanner reading from System.in is optional but safe.
  • Use a try-with-resources statement to close the Scanner automatically, even if an error occurs.
  • If you forget to close a Scanner reading from a file, the file may remain locked until your program exits.
  • Calling close() on an already-closed Scanner does nothing and causes no error.

Why you need to close a Scanner

When a Scanner reads from a file, it opens a connection to that file. Java keeps track of this connection as a resource. If you never close the Scanner, the file stays open in memory even after your code finishes reading from it. This wastes system resources and can prevent other programs from modifying or deleting the file.

Closing the Scanner tells Java to shut down that connection cleanly. The file is then free for other programs to use. This is especially important in larger programs that open many files — leaving them open can slow down your system or cause your program to run out of available file handles.

If your Scanner reads from System.in (keyboard input), closing it is less critical but still safe to do. Closing System.in will prevent further keyboard input in your program, so you would only do this if you are certain you are finished reading.

The basic close() pattern

Here is the simplest way to close a Scanner:

Scanner scan = new Scanner(new File("data.txt")); // read from scan here scan.close();

The close() call goes after you are done reading. If your code reads multiple lines in a loop, put the close() call after the loop ends. If an error occurs before you reach close(), the Scanner will not be closed — this is why the try-with-resources approach (described below) is safer.

Use try-with-resources to close automatically

Java offers a cleaner way to may provide that a Scanner closes, even if your code crashes or throws an error. This is called a try-with-resources statement. You declare the Scanner inside the parentheses after try, and Java closes it automatically when the block ends:

try (Scanner scan = new Scanner(new File("data.txt"))) { while (scan.hasNextLine()) { String line = scan.nextLine(); System.out.println(line); } } catch (FileNotFoundException e) { System.out.println("File not found"); }

The Scanner closes automatically after the closing brace of the try block, whether the code succeeded or threw an error. You do not need to write close() yourself. This pattern is the standard approach in modern Java because it prevents the common mistake of forgetting to close.

Try-with-resources works with any object that implements the AutoCloseable interface — Scanner does — so you can use this pattern with other resources like file readers and database connections too.

What happens if you do not close

If you create a Scanner that reads from a file and never call close(), the file handle remains open. In a small program that runs once and exits, the operating system cleans up automatically when the program ends. But in a long-running process or a program that opens many files, unclosed Scanners pile up and can exhaust your system's file handle limit.

You may also see warnings from your IDE or code analysis tools flagging unclosed resources. These warnings exist because leaving resources open is a sign of a bug — even if your program happens to work, it is wasting system resources.

If another part of your program tries to delete or rename a file that a Scanner still has open, the operation may fail on Windows systems, which lock files that are in use. On Unix-like systems the file can usually be deleted even while open, but the space is not freed until the handle closes.

Closing a Scanner that reads from System.in

If your Scanner reads from keyboard input using new Scanner(System.in), closing it works the same way technically — you call close() — but the consequences are different. Closing System.in closes the entire standard input stream for your program, so no further keyboard input is possible.

In most cases, you do not need to close a Scanner reading from System.in because the program will exit shortly anyway. If you do close it, make sure you are truly finished reading user input. If your program needs to read from the keyboard again later, do not close the Scanner.

Frequently Asked Questions

Can I use a Scanner after calling close()?

No. Once you call close(), the Scanner is closed and cannot read anymore. Calling methods like nextLine() or hasNextLine() on a closed Scanner will throw an IllegalStateException. If you need to read from the same file again, create a new Scanner.

What if I call close() twice?

Calling close() twice causes no error. The first call closes the Scanner; the second call does nothing because there is nothing left to close. This is why try-with-resources is safe — it calls close() automatically, but if your code also calls it manually, no harm is done.

Does close() throw an exception?

The close() method can throw an IOException in rare cases, though this is uncommon with Scanner. If you call close() outside a try-with-resources block and want to handle exceptions, wrap it in a try-catch. Most of the time, ignoring the exception is safe.

Should I close a Scanner reading from a String?

If you create a Scanner from a String using new Scanner("some text"), closing it is not necessary because there is no underlying file or stream to release. However, calling close() does no harm, so using try-with-resources is still a good habit.