Close a Scanner by calling the close() method

A Scanner in Java reads input from a source — usually the keyboard, a file, or a network connection. When you are done reading, you must close it to free up the system resources it holds. Call scanner.close() to release those resources when ready.

If you do not close a Scanner, the resource stays open until your program ends. For a single small program this may not matter. For a long-running process or one that opens many Scanners, unclosed resources pile up and slow the program down or cause it to crash.

The simplest way to close a Scanner is to call close() right after you finish reading:

Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); scanner.close();

Key Takeaways

  • Call scanner.close() when you finish reading to release system resources when ready.
  • Use try-with-resources syntax (try with parentheses) to close the Scanner automatically, even if an error occurs.
  • Closing a Scanner that reads from System.in also closes System.in itself, which can prevent further input in the same program.
  • If you open many Scanners in a loop or long-running program, closing each one prevents resource leaks.

Use try-with-resources to close automatically

Java has a built-in way to close resources without writing close() yourself. The try-with-resources statement automatically closes any resource that implements AutoCloseable — and Scanner does. Write the Scanner inside parentheses after the word try:

try (Scanner scanner = new Scanner(System.in)) {   String input = scanner.nextLine();   System.out.println(input); }

When the try block ends — whether the code finishes normally or throws an error — Java closes the Scanner for you. You do not write scanner.close() at all. This is the safest approach because it guarantees the Scanner closes even if something goes wrong inside the try block.

You can open multiple Scanners in the same try statement by separating them with semicolons:

try (Scanner file1 = new Scanner(new File("data.txt"));      Scanner file2 = new Scanner(new File("config.txt"))) {   // read from both }

Understand what closing System.in does

If your Scanner reads from System.in (the keyboard), closing it also closes System.in itself. After you close the Scanner, you cannot read from the keyboard again in the same program — any attempt to create a new Scanner from System.in will fail silently or throw an exception.

For a program that reads user input once and then exits, this does not matter. For a program that reads multiple times, you have two choices: either do not close the Scanner until the very end of the program, or create a single Scanner at the start and reuse it throughout instead of creating new ones.

If you are reading from a file instead of System.in, closing the Scanner has no side effects — you can open and close as many file Scanners as you need:

try (Scanner scanner = new Scanner(new File("input.txt"))) {   while (scanner.hasNextLine()) {     String line = scanner.nextLine();   } }

Close Scanners in loops to prevent resource leaks

If your code opens a Scanner inside a loop, close it inside the loop as well. Otherwise each iteration creates a new Scanner without closing the old one, and resources accumulate:

for (int i = 0; i < 100; i++) {   Scanner scanner = new Scanner(new File("data" + i + ".txt"));   String line = scanner.nextLine();   scanner.close(); // close inside the loop }

The try-with-resources approach is cleaner here because it closes automatically at the end of each iteration:

for (int i = 0; i < 100; i++) {   try (Scanner scanner = new Scanner(new File("data" + i + ".txt"))) {     String line = scanner.nextLine();   } }

Handle exceptions when closing

Calling close() can throw an IOException if something goes wrong with the underlying resource. In most cases you do not need to catch this exception — the Scanner is closing anyway. But if you call close() manually (not in try-with-resources), you can wrap it in a try-catch to be safe:

Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); try {   scanner.close(); } catch (Exception e) {   System.out.println("Error closing scanner: " + e.getMessage()); }

Try-with-resources handles this for you automatically, so you do not need the extra try-catch. This is another reason to prefer try-with-resources in real code.

Frequently Asked Questions

What happens if I never close a Scanner?

The Scanner stays open and holds a file handle or system resource until your program exits. In a short program this causes no visible problem. In a long-running process or one that opens many Scanners, unclosed resources accumulate and the program may run out of file handles or memory, causing a crash.

Can I use Scanner in a try-with-resources if it reads from System.in?

Yes, but remember that closing the Scanner also closes System.in. If your program needs to read from the keyboard again later, do not use try-with-resources for System.in — instead create one Scanner at the start and keep it open for the entire program.

Do I need to close a Scanner that reads from a String?

Technically no — a Scanner reading from a String does not hold any system resources. But it is good practice to close it anyway, and using try-with-resources costs nothing, so close it for consistency.

What is the difference between close() and isClosed()?

close() shuts down the Scanner and releases its resources. isClosed() is a method that returns true if the Scanner has already been closed, and false if it is still open. You can check isClosed() before calling close() to avoid closing twice, though closing an already-closed Scanner does nothing.