/* a program to read a file * @author Biagioni, Edoardo * @assignment lecture 8 * @date February 5, 2008 * @inspiration William Albritton's ReadFromFile * http://www2.hawaii.edu/~walbritt/ics211/stackArray/ReadFromFile.java */ import java.util.Scanner; import java.io.File; public class ReadFile { /* @param should be one argument, the name of the file to read */ public static void main(String[] param) { if (param.length == 1) { try { /* open the file for reading */ File file = new File(param[0]); Scanner scan = new Scanner(file); /* open successful */ System.out.println("reading file " + param[0] + ":"); int line = 1; // print a line number before each line while (scan.hasNextLine()) { System.out.println(line + ": " + scan.nextLine()); line++; } } catch (Exception e) { // what happened? System.out.println ("exception " + e + " accessing file " + param[0]); } } else { System.out.println("expected one file name as parameter, found " + param.length); } } }