IOException

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.

IOException

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 that reads from 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 syste
m cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.(Unknown Source)
        at java.util.Scanner.(Unknown Source)
        at EchoSquareDisk.main(EchoSquareDisk.java:9)

Because of this, two things are needed in the program. The main() method must start with the line:

public static void main (String[] args) throws IOException

And 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.

QUESTION 5:

Is a program that reads a single integer from a file very practical?