import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /**** * Author: G Towell * File: TextIO.java * Date: Sep 5, 2019 * Use: * Will print out the contents of a file .. one word per line * Shows use of Scanner both on file and string * As well as exception handling *****/ public class TextIO { public static void main(String[] args) { String inFileName = "liam.txt"; //Scanner input=null; String line; System.out.println("Hello"); try (Scanner input=new Scanner(new File(inFileName))){ // not use of try with resources while (input.hasNextLine()) { // test if there is a line to read // read the next line line = input.nextLine(); Scanner s2 = new Scanner(line); while (s2.hasNext()) { System.out.println(s2.next()); } s2.close(); } } catch (FileNotFoundException e) { System.out.println("Error in opening the file:" + inFileName); System.exit(1); } } }