/**
 * This class is used to read and write a file 
 * 
 * @author Jeff Borland
 * @version 12-10-11
 */
import java.io.*;
import java.util.ArrayList;
public class MyFile
{
    File theFile;
    ArrayList<String> fileContents = new ArrayList<String>();  //I use a list to store each line of the file
    int currentLocation=0;
    
    /**
     * When constructed, MyFile requires a string of the file location.  
     * If in the same folder, just put its name, otherwise put its entire path like c:\test.txt
     */
    public MyFile(String str)
    {
        theFile = new File(str);
        try{
            FileInputStream fstream = new FileInputStream(theFile);
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {
                // Print the content on the console
                fileContents.add(strLine);
            }
        }
        catch (Exception e)
        {
        }
    }

    /**
     * Returns the nextLine read from the file
     */
    public String nextLine()
    {

        if (currentLocation<fileContents.size())
        {            
            String current= fileContents.get(currentLocation);
            currentLocation++;
            return current;
        }
        else
            return "";
    }

    /**
     * Return the entire file as a string
     */
    public String allLines()
    {
        String all="";
        for (String s:fileContents)
            all+=s+"\n";
        return all;
    }

    /**
     * This will replace (or create) a file with the contents String str
     */
    public boolean replaceFile(String str)
    {
        try{            
            FileWriter fstream = new FileWriter(theFile);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(str);
            //Close the output stream
            out.close();
            return true;
        }catch (Exception e){//Catch exception if any
            return false;
        }
    }

    /**
     * Add String str to the end of the file
     */
    public boolean addToFile(String str)
    {
        fileContents.add(str);
        String all="";
        for (String s:fileContents)
            all+=s+"\n";
        try{            
            FileWriter fstream = new FileWriter(theFile);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(all);
            //Close the output stream
            out.close();
            return true;
        }catch (Exception e){//Catch exception if any
            return false;
        }
    }
    
    /**
     * Deletes a line from the file
     */
    public boolean deleteLine(int line)
    {
        fileContents.remove(line);
        currentLocation=0;
        return addToFile("");
    }
}
    