/** * FileInputDemo opens a file specified as a command-line * argument, reads the file line by line, and prints an * upper-case version of the file to standard output. * * @author Dave Musicant */ import java.util.*; import java.io.*; public class FileInputDemo { public static void main(String[] args) { // It's good to tell people when they are invoking the program // incorrectly. if(args.length != 1) { System.out.println( "Usage: java FileInputDemo someFileName" ); System.exit(1); } // You need to declare and initialize the Scanner out here // so it will still be in scope after the try/catch blocks. Scanner in = null; try { in = new Scanner(new File(args[0])); } catch(FileNotFoundException e) { System.out.println( "Can't find the file " + args[0] + "." ); System.exit(1); } // As long as there is to be something new to be read from // the file, read a line, convert to upper case, then // print. while (in.hasNext()) { String line = in.nextLine(); System.out.println(line.toUpperCase()); } in.close(); } }