/*
This program is desiged to throw exceptions and error in an ungracefull way
 */

import java.io.*;
import java.util.*; //need for the string tokenizer


class No_More_Data extends Exception
{
    public No_More_Data() { super();}
    public No_More_Data(String s) { super(s); }
}


class ReadLines
{
    StringTokenizer st;
    BufferedReader i_stream;
    BufferedWriter o_stream;

    
    ReadLines( String input_file, String output_file) throws FileNotFoundException
    {
	st = null;
	try {
	    FileReader i_file = new FileReader( input_file );
	    FileWriter o_file = new FileWriter( output_file );
	    i_stream = new BufferedReader( i_file );
	    o_stream = new BufferedWriter( o_file );
	} catch ( FileNotFoundException e ) {
	    throw e;
	}
	catch ( IOException e ) {
	    System.out.println( e.getMessage() );
	    System.out.println("Error other than file not found\n");
	    e.printStackTrace();
	}
	
    }


    ReadLines() throws FileNotFoundException
    {
	String source, destination;
	st=null;

        BufferedReader br = new BufferedReader( 
                               new InputStreamReader(System.in));
        try {
            System.out.print("Enter the input filename: " );
            source = br.readLine().trim();
            FileReader i_file = new FileReader( source );
            System.out.print("Enter the destination filename: " );
            destination = br.readLine().trim();
            FileWriter o_file = new FileWriter( destination );
            i_stream = new BufferedReader( i_file );
            o_stream = new BufferedWriter( o_file );
	} catch ( FileNotFoundException e ) {
	    throw e;
        } catch ( IOException e ) {
            System.out.print(e.getMessage() );
        }
    }


    String get_line() throws No_More_Data
    {
	String s = null;
	try {
	    if ( !i_stream.ready() ) 
		throw new No_More_Data();
	} catch ( IOException e ) {
	    System.out.println(e.getMessage() );
	    e.printStackTrace();
	}

	try {
	    s = i_stream.readLine();
	} catch ( EOFException e ) {
	    throw new No_More_Data();
	} catch ( IOException e ) {
	    System.out.println("printlin errror" + e.getMessage() );
	    e.printStackTrace();
	}
	return s;
    }

    void put_line( String s )
    {
	try {
	    o_stream.write( s );
	}
	catch ( IOException e ) {
	    System.out.println("error");
	}
    }

    void close_files()
    {
	try {
	    i_stream.close();
	    o_stream.close();
	}
	catch ( IOException e ) {
	    System.out.println("error");
	}
    }

    String get_words( String s ) throws NoSuchElementException
    {
	String t = null;

	if ( s != null ) 
	    st = new StringTokenizer(s);
	try {
	    t = st.nextToken();
	} catch ( NoSuchElementException e ) {
	    throw e;
	}
	return t;
    }

}

public class Well_behaved
{
    public static void main (String []args)
    {
	ReadLines rl;
	boolean done;

	rl = null;

	done = true;
	try {
	    rl = new ReadLines(args[0], args[1]);
	} catch (FileNotFoundException e) {
	    try {
		rl = new ReadLines();
	    }catch (FileNotFoundException f) {	
		done = false;
	    }
	} catch ( ArrayIndexOutOfBoundsException e) {
	    try {
		rl = new ReadLines();
	    }catch (FileNotFoundException f) {	
		done = false;
	    }
	}
       
	while ( !done ) {
	    done = true;
	    try {
		rl = new ReadLines();
	    }catch (FileNotFoundException e) {	
		done = false;
	    }
	}
	    

	String s, t;
	t= "";
	s= "";

	try {
	    s = rl.get_line();
	} catch ( No_More_Data e ) {
	    rl.close_files();
	    System.exit(0);
	}

	while ( true ) {	    
	    try {
		t = rl.get_words( s );
		s = null;
	    } catch ( NoSuchElementException e ) {
		try {
		    s = rl.get_line();
		} catch ( No_More_Data g ) {
		    System.out.print("End of file closeing\n");
		    rl.close_files();
		    System.exit(0);
		}
	    }   
	    System.out.println("word gotten [" + t + "]");
	}
    } 
}


