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