/* * TextSearcherUtils.java * * Created on January 14, 2003, 1:20 PM */ package project1; import java.io.*; import java.util.LinkedList; import java.util.Iterator; import java.net.URL; /** A static method used by the Text Search Oracle. * DON'T CHANGE THIS FILE! You won't be able to hand it it for Project1. * Your turned-in project must run with our original version of the file. * * This method is in a separate file for Project 1 mainly so it won't * be accidentally changed and handed it. It might be better packaged elsewhere, * such as a method of TextSearcher. * * @author dickey cse143 */ public class TextSearcherUtils { /** Name of the file containing the text to be searched. * It would make more sense for this data to be in the TextSearcher class. * It is here just to make testing more convenient. */ private static final String bookName = "ws-sonnets.txt"; //don't change this file name, //or your program won't work when we test it. public static String[] readTextIntoArray() { return readTextIntoArray(bookName); } /* There is little or no need even read this code for Project 1. * It is here to be called to help construct a TextSearcher oracle. * DON'T CHANGE IT, or your program may not run on our system. */ /** Read a text file into an array of strings. * @param bookName The name of the text file. The file needs to be somewhere * where the class loader can find it (i.e., in the class path). Try * putting the file in the directory of this package, or in the directory * above that. * @return an array of strings, each string corresponding to a line of * the file, in the same order as on the file. A null array is returned * in case the file cannot be located or opened. An array of length 0 is a legal return value, meaning that the file existed but no data was read from it. Some of the individual lines (strings) * may be empty strings, but none will be null. */ private static String[] readTextIntoArray(String bookName) { ClassLoader cLoader = ClassLoader.getSystemClassLoader(); /* //a bit of debug code URL bookURL = cLoader.getResource(bookName); if (bookURL == null) { System.out.println("Can't even get the URL"); } else { //System.out.println("URL is " + bookURL); } */ InputStream originalStream = cLoader.getResourceAsStream(bookName); if (originalStream == null) { System.out.println("Can't find the file " + bookName); return null; } //Get a buffered reader so it is easy to get one line at a time. BufferedReader textLines = new BufferedReader(new InputStreamReader(originalStream)); if (textLines == null) { System.out.println("Unable to open buffered reader from stream " + originalStream + " from file " + bookName); return null; } //We don't know how many lines there will be, //so we store them first in a list. LinkedList linesList = new LinkedList(); while (true) { //read each line of the file and save in the list. //stay in the loop until an exception or end of file occurs //save each line of the file, without examining it. try { String thisLine = textLines.readLine(); if (thisLine == null) { //must be end of file break; } linesList.add(thisLine); } catch (IOException e) { System.out.println("Stream exception; no more data will be read.\n\t" + e); break; //leave the while loop } catch (Throwable e) { System.out.println("Highly unexpected error: " + e); linesList = new LinkedList(); //set the list back to empty, in case the //error was something like "out of memory" break; //one way to leave the while loop } } //end while int lineCount = linesList.size(); System.out.println("\nNo more data on " + bookName + " after " + lineCount + " lines."); //copy the list to a string array String[] retArray = new String[lineCount]; Iterator listIter = linesList.iterator(); for (int line = 0; line < lineCount; line++) { assert listIter.hasNext(); retArray[line] = (String) listIter.next(); assert retArray[line] != null; } return retArray; //end readTextToArray } }