title: File and Scanner objects for opening and reading data in Java
body: <p>My textbook states the following:</p>
  <p><em>You can also use the <code>Scanner</code> class to read input from a file. Instead of passing <code>System.in</code> to the <code>Scanner</code> class constructor, you pass a reference to a <code>File</code> object. Here is an example:</em></p>
  <pre><code>File myFile = new File(&quot;Customers.txt&quot;);
  Scanner inputFile = new Scanner(myFile);
  </code></pre>
  <p><em>The first statement creates an instance of the <code>File</code> class. The <code>File</code> class is in the Java API, and is used to represent a file. Notice that we have passed the string &quot;<code>Customers.txt</code>&quot; to the constructor. This creates a <code>File</code> object that represents the file &quot;<code>Customers.txt</code>.&quot;</em></p>
  <p><em>In the second statement, we pass a reference to this <code>File</code> object as an argument to the <code>Scanner</code> class constructor. This creates a <code>Scanner</code> object that uses the file &quot;<code>Customers.txt</code>&quot; as its source of input.</em></p>
  <p>My question: I am unable to understand why do we need a <code>File</code> object? Can't we write the above code just as below to read files?</p>
  <pre><code>Scanner inputFile = new Scanner(&quot;Customers.txt&quot;);
  </code></pre>
  <p>If Java needs a <code>File</code> object and a <code>Scanner</code> object, how is it exactly working behind?</p>