// This program reads in two filenames from the keyboard, checks whether
// they can be used, and then copies lines from the first file to the second.
// Barry Cornelius, 17 June 2000
import java.io. BufferedReader;
import java.io. BufferedWriter;
import java.io. File;
import java.io. FileReader;
import java.io. FileWriter;
import java.io. InputStreamReader;
import java.io. IOException;
import java.io. PrintWriter;
public class CopyReadAndCheckNames
{
   public static void main(final String[] pArgs) throws IOException
   {
      final BufferedReader tKeyboard =
                    new BufferedReader(new InputStreamReader(System.in));
      final String tInputFilename = iGetInputFilename(tKeyboard);
      final String tOutputFilename = iGetOutputFilename(tKeyboard);
      iCopyFile(tInputFilename, tOutputFilename);
   }
   private static String iGetInputFilename(final BufferedReader pKeyboard)
                                                        throws IOException
   {
      String tInputFilename = null;
      while (true)
      {
         System.out.print("Type in the name of the input file: ");
         System.out.flush();
         tInputFilename = pKeyboard.readLine();
         final File tInputFile = new File(tInputFilename);
         if ( ! tInputFile.exists() )
         {
            System.out.println(tInputFilename + " does not exist");
         }
         else if ( ! tInputFile.canRead() )
         {
            System.out.println(tInputFilename + " exists but is unreadable");
         }
         else
         {
            break;
         }
      }
      return tInputFilename;
   } //BJCHEREFIRST
   private static String iGetOutputFilename(final BufferedReader pKeyboard)
                                                         throws IOException
   {
      String tOutputFilename = null;
      while (true)
      {
         System.out.print("Type in the name of the output file: ");
         System.out.flush();
         tOutputFilename = pKeyboard.readLine();
         final File tOutputFile = new File(tOutputFilename);
         if ( ! tOutputFile.exists() )
         {
            break;
         }
         else if ( ! tOutputFile.canWrite() )
         {
            System.out.println(tOutputFilename + " cannot be written");
         }
         else
         {
            System.out.print("OK to overwrite " + tOutputFilename + "? ");
            System.out.flush();
            final String tReply = pKeyboard.readLine();
            if (tReply.charAt(0)=='y') 
            {
               break;
            }
         }
      }
      return tOutputFilename;
   }
   private static void iCopyFile(final String pInputFilename,
                                 final String pOutputFilename)
                                            throws IOException
   {
      final BufferedReader tInputHandle =
                    new BufferedReader(new FileReader(pInputFilename));
      final PrintWriter tOutputHandle =
        new PrintWriter(new BufferedWriter(new FileWriter(pOutputFilename)));
      while (true)
      {
         final String tLine = tInputHandle.readLine();
         if (tLine==null)
         {
            break;
         }
         tOutputHandle.println(tLine);
      }
      tOutputHandle.close();
   }
}
