package oligo3; import java.io.File; import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; /** Types and methods for safely opening an input file for reading lines of text. * Note: Don't forget to close the BufferedReader stream after using this class. */ public class InputFile { public static File f; private static FileReader fr; public static BufferedReader br; /**Check and open file and create FileReader and BufferedReader. * @param String filename contains no the path to the file to be opened. */ public static boolean OpenOkay (String dir, String filename) throws FileNotFoundException, IOException { f = new File(dir, filename); if (f == null) { throw new IllegalArgumentException("File should not be null."); } else if (!f.exists()) { throw new FileNotFoundException (filename + ": File does not exist."); } else if (!f.isFile()) { throw new IllegalArgumentException(filename + ": Is a directory, not a file."); } else if (!f.canRead()) { throw new IllegalArgumentException(filename + ": File cannot be read."); } // Continue checking try { fr = new FileReader(f); br = new BufferedReader(fr); } catch (FileNotFoundException e) { System.err.println("File not found: " + e.getMessage()); } catch (IOException e) { System.err.println("I/O File Read Error: " + e.getMessage()); } return br.ready(); // Tells whether this stream is ready to be read. } public static String[] getArray() throws IOException { // generic and type-safe instantiation List lines = new ArrayList(); String line = null; while ((line = br.readLine())!= null) { lines.add(line.trim()); } if (br != null) br.close(); return lines.toArray(new String[lines.size()]); } }