Yes.
The file myData.txt
should exist and should be
in the same directory as the program.
If the file does not exist, the program cannot construct
a Scanner
for it.
If this happens,
the run-time Java system will throw an IOException.
In practical terms,
this means some error messages will be written and the
program will halt.
C:\Temp>java EchoSquareDisk
Exception in thread "main" java.io.FileNotFoundException:
myData.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at EchoSquareDisk.main(EchoSquareDisk.java:9)
The main()
method must start with the line:
public static void main (String[] args) throws IOException
The Java io package must be imported:
import java.io.*;
Later chapters further discuss exceptions and what to do with them. For now, expect your program to halt if it cannot find the disk file or if something else goes wrong with I/O.
(Thought Question: ) What happens if the input file contains the digits of a very large integer, say
9000000000