import java.io.*;
/**
 * Read from a file, update it, and write it back
 *    
 * Read entire file into an array  
 * Update is done in the array
 * Array is written back to the file (same or different file)
 */

public class IOupdate
{
    public static void main(String[] args)
    {

        String from_str = (args.length > 0) ? args[0] : "Sam";       //if arg0, take it, otherwise use "Sam"
        String to_str   = (args.length > 1) ? args[1] : "Samuel";    //if arg1, take it, otherwise use "Samuel"

        String input_file  = "../data/zAddrBook.txt";            	//relative path 
        String output_file = "../data/zAddrBook.txt";            	//same file for both input & output 

//      String input_file  = "/home/sultans/web/java/demo/8inpout/data/zAddrBook.txt"; 
//      String output_file = "/home/sultans/web/java/demo/8inpout/data/zAddrBook.txt"; 
        
        String[] lines  = new String[1000];                   //create an array of 1000 lines

        read(input_file, lines);                              //read the file in a String array
        update(lines, from_str, to_str);                      //update the array
        write(output_file, lines);                            //write the file from the array   
            
        System.out.println("Update Successful");
    }    
    //---------------------- Read ---------------------------------------------------
    static void read(String input_file, String[] lines) 
    {
        try 
        {
            FileReader infile        = new FileReader(input_file);      //open infile
            BufferedReader inBuffer  = new BufferedReader(infile);      //buffer the input

            int i=0;
            while(true)
            {
                String input = inBuffer.readLine();         //read a line
                if (input == null) break;                   //check end-of-file?
                lines[i++] = input;                         //add line to the array
            }                    
            infile.close();
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }                     
    //---------------------- Update ---------------------------------------------------
    static void update(String[] lines, String from_str, String to_str)
    {
        int line_num=0;
        for (String line : lines)                               //loop thru all lines in array
        {
            if (line == null) break;                            //if null element -> break
                
            String tokens[] = line.split("\\s*,\\s*");          //split line into token array (on space comma space)
            
            for (int i=0; i<tokens.length; i++)
            {
                if (tokens[i].equals(from_str))                 //if equal to the "from_str" word
                {    
                    tokens[i] = to_str;                         //change it to the "to_str"
                    line = "";
                    for (String token : tokens)                 //loop thru the tokens array  
                        line += token + ",";                    //re-join all the tokens back into the line
                    lines[line_num] = line;                     //update the array of lines
                }
            }
            line_num++;
        }
    }

/*
            if (tokens[1].indexOf("Sam") == 0)                              //if first name starts with Sam
            {
                if      (tokens[1].equals("Sam"))    tokens[1] = "Samuel";  //if Sam, make it Samuel 
                else if (tokens[1].equals("Samuel")) tokens[1] = "Sam";     //if Samuel, make it Sam
                                
                line = "";
                for (String token : tokens)                     //loop thru the tokens array  
                {
                    line += token + ",";                        //join all the tokens into the line
                    lines[i] = line;                            //update the array of lines
                }
            }   

            i++ ;
        }
    }
*/
    //---------------------- Write ---------------------------------------------------
    static void write(String output_file, String[] lines) 
    {
        try
        {
            FileWriter  outfile   = new FileWriter(output_file);    //open outfile
            PrintWriter outBuffer = new PrintWriter(outfile);       //buffer the output

            for (String line : lines)                               //loop thru all lines in array
            {
                if (line == null) break;                            //if null element -> break                                         
                outBuffer.println(line);                            //write to file
            }            
            outfile.close();
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
    //--------------------------------------------------------------------------------
}