1 package arrayplay; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.io.PrintWriter; 7 import java.util.Scanner; 8 9 public class ReverseCopy { 10 11 public static void main(String[] args) throws FileNotFoundException, IOException{ 12 13 String inputFileName = "songLyrics.txt"; 14 String outputFileName = "songReversed.txt"; 15 String[] words = readWordsFromFile(inputFileName); 16 writeWordsToFile(words, outputFileName); 17 18 } 19 20 private static final int LIMIT = 1000; 21 22 private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException { 23 Scanner scanner = establishScanner(inputFileName); 24 String[] temp= new String[LIMIT]; 25 int index = 0; 26 while (scanner.hasNext()) { 27 String word = scanner.next(); 28 temp[index] = word; 29 index ++; 30 } 31 32 int wordCount = index; 33 String[] words = new String[wordCount]; 34 for (int x = 0; x < wordCount; x++) { 35 words[x] = temp[x]; 36 } 37 return words; 38 39 } 40 41 private static void writeWordsToFile(String[] words, String outputFileName) throws IOException { 42 PrintWriter printer = getPrintWriter(outputFileName); 43 for (int x = words.length - 1; x >= 0; x = x - 1) { 44 printer.println(words[x]); 45 } 46 printer.close(); 47 } 48 49 private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException { 50 String fullFileName = createFullFileName(outputFileName); 51 PrintWriter printer = new PrintWriter(fullFileName); 52 return printer; 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 private static String createFullFileName(String fileName) { 61 String separator = System.getProperty("file.separator"); 62 String home = System.getProperty("user.home"); 63 String path = home + separator + "CS1Files" + separator + "data" + separator; 64 String fullFileName = path + fileName; 65 return fullFileName; 66 } 67 68 69 } 70