Page 608 - Introduction to Programming with Java: A Problem Solving Approach
P. 608
574 Chapter 14 Exception Handling
/***********************************************************
*
PrintLineFromFile.java
Dean & Dean
This opens existing text file and prints a line from it.
***********************************************************/
*
*
*
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
public class PrintLineFromFile
{
}
// end PrintLineFromFile class
public static void main(String[] args)
{
}
// end main
Scanner stdIn = new Scanner(System.in);
String fileName;
// name of target file
BufferedReader fileIn; // target file
String line;
// first line from fileIn
System.out.print("Enter a filename: ");
fileName = stdIn.nextLine();
try Apago PDF Enhancer {
fileIn = new BufferedReader(new FileReader(fileName));
line = fileIn.readLine();
System.out.println("Line 1:\n" + line);
// end try
catch (Exception e)
}
{
}
System.out.println(e.getMessage());
Sample session #1:
Enter a file name: PrintLineFromFile.java
Line 1:
/***********************************************************
Sample session #2:
Enter a filename: garbage
garbage (The system cannot find the file specified)
Figure 14.12 PrintLineFromFile program—a simple file-reader