<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Siva Surya</title>
    <description>The latest articles on DEV Community by Siva Surya (@siva_surya_3ccc73ef7c9833).</description>
    <link>https://dev.to/siva_surya_3ccc73ef7c9833</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3447975%2F573c08ec-a8e8-4972-90b2-ecedba09d3b2.png</url>
      <title>DEV Community: Siva Surya</title>
      <link>https://dev.to/siva_surya_3ccc73ef7c9833</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/siva_surya_3ccc73ef7c9833"/>
    <language>en</language>
    <item>
      <title>System.in, Scanner, and File Descriptors – JAVA Case study</title>
      <dc:creator>Siva Surya</dc:creator>
      <pubDate>Tue, 04 Nov 2025 00:33:30 +0000</pubDate>
      <link>https://dev.to/siva_surya_3ccc73ef7c9833/systemin-scanner-and-file-descriptors-java-case-study-h2p</link>
      <guid>https://dev.to/siva_surya_3ccc73ef7c9833/systemin-scanner-and-file-descriptors-java-case-study-h2p</guid>
      <description>&lt;p&gt;Hello, Everyone, I am currently learning &lt;strong&gt;Java&lt;/strong&gt;. As a beginner, I worked with &lt;strong&gt;console IO&lt;/strong&gt;. To read input from the terminal, I use the &lt;strong&gt;Scanner&lt;/strong&gt; class.&lt;/p&gt;

&lt;p&gt;I tried a mini CLI project, so I used the &lt;strong&gt;Scanner&lt;/strong&gt; object in different classes. For resource management, the Scanner object — especially when reading from sources like &lt;strong&gt;System.in&lt;/strong&gt; or files — utilizes underlying system resources (like file descriptors or input streams). Closing the &lt;strong&gt;Scanner&lt;/strong&gt; explicitly releases these resources back to the operating system, preventing resource leaks.&lt;/p&gt;

&lt;p&gt;So, I closed the Scanner in each class, but after running my program, I got this exception:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Exception in thread "main" java.util.NoSuchElementException
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was the first time I saw this exception. After reading some related Stack Overflow discussions, I understood the real reason.&lt;/p&gt;




&lt;h3&gt;
  
  
  The Core Problem: Input Stream Closure
&lt;/h3&gt;

&lt;p&gt;When a &lt;strong&gt;Scanner&lt;/strong&gt; is closed, it also closes the &lt;strong&gt;underlying input stream&lt;/strong&gt; it's connected to.&lt;br&gt;
For example, if you create a Scanner from a FileInputStream, closing the Scanner will also close that FileInputStream. This ensures that all related resources are properly shut down.&lt;/p&gt;

&lt;p&gt;But I wondered:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“If I create a &lt;em&gt;new Scanner&lt;/em&gt; again, shouldn’t the JVM reopen the input stream?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Surprisingly, &lt;strong&gt;no&lt;/strong&gt; — and here’s why.&lt;/p&gt;


&lt;h3&gt;
  
  
  Example Code
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Scanner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scan1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;ip1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scan1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;scan1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scan2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;ip2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scan2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// throws Runtime exception&lt;/span&gt;
        &lt;span class="n"&gt;scan2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Exception in thread "main" java.util.NoSuchElementException
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  What Actually Happens Under the Hood
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;System.in&lt;/strong&gt; is a &lt;strong&gt;singleton InputStream&lt;/strong&gt; provided by the JVM — not something that can be reopened automatically.&lt;/li&gt;
&lt;li&gt;When you call &lt;code&gt;scan1.close()&lt;/code&gt;, it calls &lt;code&gt;System.in.close()&lt;/code&gt; internally.&lt;/li&gt;
&lt;li&gt;This closes the &lt;strong&gt;standard input stream&lt;/strong&gt; permanently for that JVM session.&lt;/li&gt;
&lt;li&gt;Any subsequent Scanner (like &lt;code&gt;scan2&lt;/code&gt;) tries to read from a &lt;strong&gt;closed input stream&lt;/strong&gt;, so the JVM throws a &lt;strong&gt;NoSuchElementException&lt;/strong&gt; or &lt;strong&gt;IllegalStateException&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So even though you created a &lt;em&gt;new&lt;/em&gt; Scanner, it still points to the &lt;strong&gt;same closed System.in&lt;/strong&gt;, not a fresh one.&lt;/p&gt;




&lt;h3&gt;
  
  
  What’s the Correct Practice?
&lt;/h3&gt;

&lt;p&gt;If you’re reading from &lt;strong&gt;System.in&lt;/strong&gt;, &lt;strong&gt;never close the Scanner&lt;/strong&gt; until your entire program finishes reading all input.&lt;br&gt;
You can safely reuse the same Scanner across methods or classes by &lt;strong&gt;passing it as a reference&lt;/strong&gt; — but avoid closing it.&lt;/p&gt;

&lt;p&gt;Example fix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Scanner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;scan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;in&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;ip1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scan&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;ip2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scan&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;scan&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Close only once at the end&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  JVM and OS-Level Insight
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;System.in&lt;/strong&gt; is typically linked to &lt;strong&gt;stdin&lt;/strong&gt; (file descriptor 0) at the OS level.&lt;/li&gt;
&lt;li&gt;Once closed, the OS releases that descriptor, and the JVM does not recreate it automatically.&lt;/li&gt;
&lt;li&gt;Creating a new Scanner doesn’t reopen the descriptor — it just wraps the already-closed stream.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Key Takeaway
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;In Java, &lt;strong&gt;closing a Scanner created from System.in closes the entire standard input stream&lt;/strong&gt; for the running JVM.&lt;br&gt;
Always close it &lt;strong&gt;only once&lt;/strong&gt;, at the end of the program — or better, let the JVM handle it automatically when the program exits.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;That’s what I discovered in my small case study — a good reminder that &lt;strong&gt;not all resources can be reopened once closed&lt;/strong&gt;, especially when they represent system-level standard streams.&lt;/p&gt;

</description>
      <category>java</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
