ReverseCopy.java
package arrayplay;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

/* 
program featuring straight up arrays and file Io to read and reverse copy a lyric. 
 */

public class ReverseCopy {
   public static void main(String[] args) throws FileNotFoundException, IOException {
      String inputFileName = "caughtup.txt";
      String outputFileName = "caughtupReversed.txt";
      String[] words = readWordsFromFile(inputFileName);
      writeWordsToFile(words,outputFileName);
   }

   private static final int LIMIT = 1000;



   private static String[] readWordsFromFile(String inputFileName)  throws FileNotFoundException {
      //equate a scanner with an input file
      Scanner scanner = establishScanner(inputFileName);
      //read the words from the file into an oversized array
      String[] temp = new String[LIMIT];

      int index = 0;

      while (scanner.hasNext() ) {
         String word = scanner.next();
         temp[index] = word;
         index += 1;
      }

      int wordCount = index;
      //transfer the words to a perfectly sized array
      String[] words = new String[wordCount];
      for (int x = 0; x < wordCount; x += 1) {
        words[x] = temp[x];
      }

      return words;
   }

   private static void writeWordsToFile(String[] words, String outputFileName) throws IOException {
      //equate a printer with an output file
      PrintWriter printer = getPrintWriter(outputFileName);
      //print the words to the file
      for (int x = (words.length -1); x >= 0; x -= 1) {
         printer.println(words[x]);
      }
      printer.close();
   }

   private static Scanner establishScanner(String inputFileName) throws FileNotFoundException  {
      String fullFileName = createFullFileName(inputFileName);
      return new Scanner(new File(fullFileName));

   }



   private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
      String fullFileName = createFullFileName(outputFileName);
      PrintWriter printer = new PrintWriter(fullFileName);
      return printer;
   }

   //create the full file name for a simple file name assuming that it will be found in the CS1Files/data directory
   private static String createFullFileName(String inputFileName) {
      String separator = System.getProperty("file.separator");
      String home = System.getProperty("user.home");
      String path = home + separator + "CS1Files" + separator + "data" + separator;
      String fullFileName = path + inputFileName;
      return fullFileName;
   }

}