import java.io.*;
import java.util.Scanner;
/**
 * THFileIO.java - Handles reading from and writing to text files.
 * @author Thomas
 */
public class THFileIO {
	private PrintWriter output;
	private FileInputStream file;
	//Scanner is also a buffered stream reader, so HA!
	private Scanner input;
	private String filename;
	private boolean append = false;
	/**
	 * Constructor for THFileIO.
	 * @param the file name to interact with.
	 */
	public THFileIO(String name){
		filename = name;
		try {
			file = new FileInputStream(filename);
			input = new Scanner(file);
			returnToStart();
		}
		catch (FileNotFoundException e){
			try{
				(new File(filename)).createNewFile();
				file = new FileInputStream(filename);
				input = new Scanner(file);
				returnToStart();
			} catch(IOException f) {f.printStackTrace();}
		}
	}
	public THFileIO(String name, boolean app){
		this(name);
		append = app;
	}
	/**
	 * Returns the reader to the beginning of the file.
	 */
	public void returnToStart(){
		try {file = new FileInputStream(filename);}
		catch (FileNotFoundException e){}
		
		try{input = new Scanner(file);}
		catch (NullPointerException n){}
	}
	/**
	 * Reads the next line from the file.
	 * @return The line read. Returns null if no more lines.
	 */
	public String readFromFile(){
		String result = null;
		if (input.hasNext()){
			result = input.nextLine().trim();
			while(result.length() > 0 && result.charAt(0) == '\t'){
				if (result.equals("\t"))
					result = "";
				else
					result = result.substring(1, result.length() - 1);
			}
		}
		return result;
	}
	/**
	 * Gets a line off of the file and returns it.
	 * @param line number of the list to read.
	 * @return String that contains that line of the file.
	 */
	public String readLine(int line){
		String result;
		returnToStart();
		
		//Get to the line we want to read.
		for (int count = 0; count < line - 1; count++)
			if (input.hasNext())
				result = input.nextLine();
		// Put the default in result.
		result = null;
		//If there is something at the line given, read it.
		if (input.hasNext())
			result = input.nextLine();		
		return result;
	}
	/**
	 * Writes a line to the end of the file. When you are completely
	 * done writing to the file, make sure to call finishWriting()
	 * @param String that contains the text to write.
	 * @return void
	 */
	public void writeToFile(String outputstring){
		if (output == null){
			try {output = new PrintWriter(new FileWriter(filename, append), true);}
			catch (IOException e){}
		}
		output.println(outputstring);
	}
	/**
	 * Writes a line to the end of the file. When you are completely
	 * done writing to the file, make sure to call finishWriting()
	 * @param String that contains the text to write.
	 * @return void
	 */
	public void writeToFile(){
		writeToFile("");
	}	
	/**
	 * Checks if there are more lines to read in the file.
	 * @return true if there are more lines to read.
	 */
	public boolean hasNext(){
		return input.hasNext();
	}
	/**
	 * "Saves" the written file. If this is not called, the
	 * data written to the file will not be saved after the
	 * program exits.
	 */
	public void finishWriting(){
		if (output != null)
			output.close();
		System.gc();
	}
	public void restartWriting(){
		try {output = new PrintWriter(new FileWriter(filename, append), true);}
		catch (IOException e){}
	}
}
