package msoe.se1011.bioanalyzer; /* * Created on Apr 26, 2005 * * FileReader.java * @author: hornick */ /** * @author hornick * * */ import java.io.*; import java.text.*; import java.util.*; /** * A simple data file reader (that hides a lot of file-related I/O from the user of this class) * @author hornick * */ public class FileReader { // These are constants values returned by the getStutus method (see below) // Note: We're doing something very unusual here, in that we're generating a random // value between 0 and 100 that we're initializing the first constant to, so // that you (the student) cannot assume that these constants have any specific values. public static final int NOT_INITIALIZED = (int)( 100*Math.random()); public static final int NO_SUCH_FILE = NOT_INITIALIZED+1; public static final int BAD_DATA = NOT_INITIALIZED+2; public static final int DATA_READ_OK = NOT_INITIALIZED+3; private static int status = NOT_INITIALIZED; // the status of the last method called; one of the above constants private static int numValues = 0; // the number of values in the String /** * Constructor for a FileReader object * The constructor is private, since there is no need to allow instantiation, as all methods are static. */ private FileReader() { // nothing to do; there are no instance attributes to initialize } /** * Accessor * @return the current status of the FileReader, as defined by one of the following: * NOT_INITIALIZED - readFileIntoString() has not been called yet * NO_SUCH_FILE - invalid file passed to readFileIntoString * BAD_DATA - corrupted data in the file * DATA_READ_OK - all data read was OK */ public static int getStatus() { return status; } /** * Accessor * @return the number of values read from the file during the readFileIntoString method (see below) */ public static int getNumberOfValuesRead() { return numValues; } /** * Reads the entire contents of the specified file into a String * @param filename the file to be read * @return a String containing the entire contents of the file, or a null if an error occurred. * Check the success or failure of this method by calling the getStatus() method, which * will return NOT_INITIALIZED, NO_SUCH_FILE, or BAD_DATA in the case of a failure, * and DATA_READ_OK if the file was read successfully. */ public static String readFileIntoString( String filename ) { File dataFile = new File( filename ); if( !dataFile.exists() ) { status = NO_SUCH_FILE; return null; // return a null string if no such file exists } Scanner scanner; // we'll use this Scanner to read the file numValues = 0; // this counts the number of values we'll read from the file try { scanner = new Scanner(dataFile); // scanner.useDelimiter("[~(\r\n]"); } catch( FileNotFoundException e ){ // no such file to scan, or no permission to read it status = NO_SUCH_FILE; return null; // return a null string if no such file exists } String bigString = ""; // the string being created try { while( scanner.hasNext() ) { // keep reading as long as there is something left to read... String data = scanner.next(); int value = Integer.parseInt( data ); bigString += String.format("%d ", value ); numValues++; } status = DATA_READ_OK; return bigString; } catch( Exception e ) { // oh no... status = BAD_DATA; return null; // on read error, return a null string } } }