/* * Program featuring straight up arrays and file IO to read and reverse copy a lyric. */ package arrayplay; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class ReverseCopy { public static void main(String[] args) throws FileNotFoundException, IOException{ String inputFilename = "Africa.text"; String outputFileName = "AfricaReversed.text"; String[] words = readWordsFromFile(inputFilename); writeWordsToFile(words,outputFileName); } 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 = x - 1){ printer.println(words[x]); } printer.close(); } private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException{ String fullFileName = createFullFileName(outputFileName); PrintWriter printer = new PrintWriter(fullFileName); return printer; } private static String createFullFileName(String fileName) { String separator = System.getProperty("file.separator"); String home = System.getProperty("user.home"); String path = home + separator + "CS1Files" + separator + "data" + separator; String fullFileName = path + fileName; return fullFileName; } private static final int LIMIT = 1000; private static String[] readWordsFromFile(String inputFilename) throws FileNotFoundException { //Equate a scanner wit hthe input file Scanner scanner = establishScanner(inputFilename); //Read the words from the file into and oversized array String[] temp = new String[LIMIT]; int index = 0; while (scanner.hasNext()){ String word = scanner.next(); temp[index] = word; index = 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 = x + 1){ words[x] = temp[x]; } //return the words return words; } private static Scanner establishScanner(String inputFilename) throws FileNotFoundException{ String fullFilename = createFullFileName(inputFilename); return new Scanner(new File(fullFilename)); } }