import java.util.*; import java.io.*; class UseScanner { // This method reads a text file and prints its contents to the screen // it illustrates using the scanner class. // the name of the text file is taken from the input arguments // or input by the user. public static void main(String[] args) throws FileNotFoundException { // Now get a filename to read unless it was given in a command line argument. Scanner in = new Scanner(System.in); System.out.println(); String filename; if (args.length==0) { System.out.println("Enter a filename to read:"); filename = in.nextLine(); } else filename = args[0]; // read from a file line by line, printing out to screen. System.out.println("Reading file " + filename + " and printing to screen."); Scanner filescan = new Scanner(new File(filename)); while (filescan.hasNextLine()) System.out.println(filescan.nextLine()); filescan.close(); } }