Close a Scanner by Calling the close() Method
To close a Scanner in Java, call the close() method on your Scanner object. This tells Java to release the resources the Scanner is using — typically the input stream it was reading from, like a file or the keyboard.
Here is the basic pattern:
Scanner scanner = new Scanner(System.in); // ... use the scanner ... scanner.close();
Once you call close(), that Scanner object cannot be used again. If you try to read from it after closing, Java will throw an exception.
Key Takeaways
- Call scanner.close() when you are finished reading input to free up system resources.
- A closed Scanner cannot be reused; trying to read from it will cause an exception.
- Use a try-with-resources statement to close the Scanner automatically, even if an error occurs.
- Closing System.in (the keyboard input stream) is optional in most programs, but closing file-based Scanners is important to avoid resource leaks.
Why You Should Close a Scanner
When you create a Scanner, Java opens a connection to whatever stream you gave it — a file, the keyboard, a network socket, or something else. That connection uses system resources. If you never close the Scanner, the connection stays open until your program ends, and the operating system has to clean it up.
In a small program that runs once and exits, this is usually harmless. In a long-running process or a program that opens many Scanners, unclosed connections pile up and can exhaust the system's file descriptor limit. On some systems, this causes the program to crash or refuse new connections.
Closing the Scanner also flushes any buffered data and ensures the underlying stream is properly shut down. This is especially important when reading from files — a closed file Scanner guarantees the file is released and can be deleted or moved by other programs.
Use Try-With-Resources to Close Automatically
The safest way to use a Scanner is to wrap it in a try-with-resources statement. Java will close the Scanner automatically when the block exits, even if an exception is thrown.
try (Scanner scanner = new Scanner(System.in)) { String line = scanner.nextLine(); System.out.println(line); } // Scanner is closed here automatically
This pattern is cleaner than calling close() by hand because you cannot forget to close, and the Scanner is closed even if nextLine() or another method throws an exception. Try-with-resources works with any object that implements the AutoCloseable interface, and Scanner does.
Closing a File-Based Scanner
When you open a Scanner to read from a file, closing it is more important than when reading from the keyboard. A file Scanner holds a lock on the file, and the operating system may not let other programs access it until the Scanner is closed.
try (Scanner scanner = new Scanner(new File("data.txt"))) { while (scanner.hasNextLine()) { System.out.println(scanner.nextLine()); } } // File is released here
If you do not close a file Scanner and then try to delete, rename, or move the file in your code, the operation may fail on Windows because the file is still open. On Linux and macOS, the file can usually be deleted while open, but the space is not freed until the Scanner closes.
What Happens If You Do Not Close
In a typical small program, forgetting to close a Scanner causes no visible problem. The Scanner closes when the program exits, and the operating system reclaims the resources.
In a server process, a web service, or any program that creates many Scanners over time, unclosed Scanners accumulate. Eventually, the program runs out of file descriptors — the operating system's limit on how many open files a single process can have. At that point, new Scanners cannot be created, and the program crashes or hangs.
This is why try-with-resources is the standard practice: it prevents the mistake from happening in the first place, and it costs nothing in performance.
Closing System.in Is Usually Optional
If your Scanner reads from System.in (the keyboard), closing it is optional in most cases. System.in is a shared resource managed by the Java runtime, and closing a Scanner that wraps it does not actually close the underlying stream — it just closes the Scanner's wrapper.
However, there is a catch: once you close a Scanner wrapping System.in, you cannot create a new Scanner wrapping System.in in the same program. If you do, the new Scanner will not work because the underlying stream is marked as closed.
For this reason, many programs that read from the keyboard create a single Scanner at the start and reuse it throughout, rather than closing it and creating a new one. If you do use try-with-resources with System.in, make sure you do not need to read from the keyboard again later.
Frequently Asked Questions
What happens if I call close() twice?
Calling close() twice is safe. The second call does nothing because the Scanner is already closed. Java does not throw an exception.
Can I reopen a Scanner after closing it?
No. Once closed, a Scanner cannot be reused. You must create a new Scanner object if you need to read again. If you are reading from a file, you would create a new Scanner wrapping a new File object.
Does closing a Scanner close the underlying file?
Yes. When you close a Scanner that wraps a file, the file is closed and released. Other programs can then access, delete, or move the file.
Should I close a Scanner if my program is about to exit?
It is good practice to close all Scanners before exit, even in a short program. The habit prevents bugs when you reuse code in a larger process. Try-with-resources makes this automatic and costs nothing.
What is the difference between close() and flush()?
A Scanner does not have a flush() method. close() closes the Scanner and the underlying stream. If you need to flush buffered output to a file or stream, you would call flush() on the output stream itself, not on a Scanner.