Input Stream from a File

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

Yes. In fact, this is what is done with input redirection.

Input Stream from a File

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:

File    file = new File("myData.txt");   // create a File object
Scanner scan = new Scanner( file );      // connect a Scanner to the file

These statements first create a File object. This is a software object that represents a disk file. Next a Scanner object is constructed and connected to the actual file.

scanner reading integers

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.

QUESTION 3:

Must the disk file that is used with Scanner be a text file?