import java.io.*;

public class FileHandler
{	
	private BufferedReader myReader;
	private boolean bMyEndOfFileReachedStatus;
	private String myFileName;

	/**
	 * constructor with file name specified.<br>
	 * pre: fileName specifies the full path to the file and
	 * that file exists. 
	 * post: conncetion to file made. ready to call readSingleChar or readLine
	 * @param fileName a String with the full path to the file
	 */	
	public FileHandler(String fileName)
	{	setNewFile(fileName);
	}

	/**
	 * returns the rest of the current line from the file.
	 * If 1 or more characters from the line have already been read only the
	 * remaining characters are read.<br>
	 * pre: getAtEndOfFile() == false<br>
	 * post: return the rest of the current line as a String. if null returned
	 * getAtEndOfFile() == true
	 * @return a String with the rest of the current line.  The end of line
	 * chracter is not included at the end of the String. If the file has been
	 * exhausted or an error occurs returns null
	 */	
	public String readLine()
	{	String result = null;
		try
		{	result = myReader.readLine();
			if(result == null)
			{	myReader.close();
				bMyEndOfFileReachedStatus = true;
			}
		}
		catch(IOException e)
		{	System.out.println("An error occured while trying to read");
			System.out.println("from the file " + myFileName + ". The error was");
			System.out.println(e);
			return null;
		}
		return result;
	}

	/**
	 * returns the next character from the file.
	 * pre: getAtEndOfFile() == false<br>
	 * post: return the integer Unicode code for the next character in the file
	 * or -1 if end of file reached if -1 returned getAtEndOfFile() == true
	 * @return an int, the Unicode code for the next character in the file or
	 * -1 if no 
	 */	
	public int readNextCharacter()
	{	int result = -1;
		try
		{	result = myReader.read();
			if(result == -1)
			{	myReader.close();
				bMyEndOfFileReachedStatus = true;
			}
		}
		catch(IOException e)
		{	System.out.println("An error occured while trying to read");
			System.out.println("from the file " + myFileName + ". The error was");
			System.out.println(e);
			return -1;
		}
		return result;
	}

	/**
	 * reset the file to the beginning of the file<br>
	 * pre: none<br>
	 * post: ready to read first character from the file, getAtEndOfFile() == false
	 */
	public void reset()
	{	setNewFile(myFileName);
	}
	
	/**
	 * change the file to be read from to the file name specified.<br>
	 * pre: fileName specifies the full path to a file and
	 * that file exists. 
	 * post: conncetion to file made. ready to call readSingleChar or readLine
	 * @param fileName a String with the full path to the file
	 */		
	public void setNewFile(String fileName)
	{	myFileName = fileName;
		bMyEndOfFileReachedStatus = false;
		try
		{	myReader = new BufferedReader(new FileReader(myFileName));
		}
		catch(IOException e)
		{	System.out.println("An error occured while trying to connect");
			System.out.println("to the file " + myFileName + ". The error was");
			System.out.println(e);
		}
	}

	/**
	 * has the end of the file been reached?<br>
	 * pre: none<br>
	 * post: return true if end of file not reached yet.
	 */	
	public boolean atEndOfFile()
	{	return bMyEndOfFileReachedStatus;	}
}
