/** * This program reads Strings from an ascii file. */ import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.ArrayList; public class FileIO { /** Read Strings from the file. Assumes each line of the file contains a single String. * @param filename the name of the ascii file name */ public static ArrayList readFile(String filename) { ArrayList dataArray = new ArrayList(); try { File inputFile = new File(filename); Scanner in = new Scanner(inputFile); int cnt = 0; while(in.hasNextLine()) { dataArray.add(new Animal(in.nextLine().trim())); cnt++; } System.out.println("count = " + cnt); } catch (FileNotFoundException e) { System.out.println("File not found."); } return dataArray; } // Used only for testing. Assumes there is a file called animals.txt that is stored // in the same folder as this java file. public static void main(String[] args) { ArrayList words = FileIO.readFile("animals.txt"); System.out.println(words.toString()); } }