1 /* 2 * A program to print lyrics from Coldplay - Fix You, backwards using an Array. 3 * Created by Hunter Gersitz | Oswego Fall 2019 4 * */ 5 6 7 package arrayPlay; 8 9 import java.io.File; 10 import java.io.FileNotFoundException; 11 import java.io.IOException; 12 import java.io.PrintWriter; 13 import java.util.Scanner; 14 15 public class ReverseCopy { 16 private static final int LIMIT = 1000; 17 18 public static void main(String[] args) throws FileNotFoundException, IOException { 19 String inputFileName = "lyricsBob.txt"; 20 String outputFileName = "lyrics_return.txt"; 21 String[] words = readWordsFromFile(inputFileName); 22 writeWordsToFile(words, outputFileName); 23 } 24 25 private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException { 26 String[] temp = new String[LIMIT]; 27 int index = 0; 28 Scanner scanner = establishScanner(inputFileName); 29 while ( scanner.hasNext() ) { 30 String word = scanner.next(); 31 temp[index] = word; 32 index = index + 1; 33 } 34 int wordCount = index; 35 // Transfer the words 36 String[] words = new String[wordCount]; 37 38 for ( int x=0; x < wordCount; x = x+1 ) { 39 words[x] = temp[x]; 40 } 41 // Return the words 42 return words; 43 } 44 45 private static void writeWordsToFile(String[] words, String outputFileName) throws IOException { 46 PrintWriter printer = getPrintWriter(outputFileName); 47 // Print the words to file 48 for ( int x = words.length-1; x >=0; x=x-1 ) { 49 printer.println(words[x]); 50 } 51 printer.close(); 52 53 } 54 55 private static Scanner establishScanner(String inputFileName) throws FileNotFoundException { 56 String fullFileName = createFullFileName(inputFileName); 57 return new Scanner(new File(fullFileName)); 58 } 59 60 61 private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException { 62 String fullFileName = createFullFileName(outputFileName); 63 PrintWriter printer = new PrintWriter(fullFileName); 64 return printer; 65 } 66 67 private static String createFullFileName(String fileName) { 68 String separator = System.getProperty("file.separator"); 69 String home = System.getProperty("user.home"); 70 String path = "data" + separator; 71 String fullFileName = path + fileName; 72 return fullFileName; 73 } 74 } 75