Yes. In fact, this is what is done with input redirection.
With file input redirection, a disk file is connected to the
standard input stream used by a Scanner
object.
You can also construct a scanner object that connects to
a disk file:
// create a File object File file = new File("myData.txt"); // connect a Scanner to the file Scanner scan = new Scanner( file );
These statements first create a File
object.
This is a software object that represents a file name.
Creating a File
object does not create a disk file.
Next a Scanner
object is constructed and connected
to the actual file.
The same methods can be used with this Scanner
object
as with standard input.
Characters than can be converted into an int
can be scanned in
using nextInt()
as with the standard input stream.
Bug Alert! The following will not work:
Scanner scan = new Scanner( "myData.txt" );
This creates a Scanner
that scans through the String
given as its argument.
Must the disk file used with Scanner
be a text file?