//Alex Roque
//Database Systems
//Dec. 6,2000
//This file is REQUIRED to print the transcript to a file, it must be in the same directory,
//and must be included in the same project (and added)

import java.io.*;

/** A class to open files for input/output after prompting for the file name
 */

public class OpenFiles {

    static String getFileName() throws IOException {
        
        BufferedReader cin = new  BufferedReader(new InputStreamReader(System.in));
        return cin.readLine();
    
    } // end getFileName
    
    /** Prompts the user for a file name and opens the file for input
     *@return the opened file
     */

    public static FileReader getNameAndOpenInput() throws IOException {
    
        int tries = 1;
        String fileName;
        
        while(true) {
                System.out.print("Enter the input file name> ");
                System.out.flush();
                fileName = getFileName();
                try {
                        return new FileReader(fileName);
                } catch( FileNotFoundException e ) {
                        if( tries > 2 ) throw e;
                        System.out.println(fileName + " could not be opened. Try again.");
                        tries++;
                        if( tries == 3 ) System.out.println("This is your last try!");
                } // try/catch          
        } // end while
   
   } // end getNameAndOpenInput

    /** Prompts the user for a file name and opens the file for output
     *@return the opened file
     */

   public static FileWriter getNameAndOpenOutput() throws IOException {
        
        String fileName;
        fileName = "out.txt" ;
        /*
        int tries = 1;
   
        while(true) {
                System.out.print("Enter the output file name> ");
                System.out.flush();
                fileName = getFileName();
                try {
                        return new FileWriter(fileName);
                } catch( IOException e ) {
                        if( tries > 2 ) throw e;
                        System.out.println(fileName + " could not be opened. Try again.");
                        tries++;
                        if( tries == 3 ) System.out.println("This is your last try!");
                } // try/catch          
       
        } // end while

        */
        return new FileWriter(fileName);
    } // end getNameAndOpenOutput

} // end OpenFiles




