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