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 String inputFileName = "Promises.text"; 13 String outputFileName = "PromisesReversed.text"; 14 String[] words = readWordsFromFile(inputFileName); 15 writeWordsToFile(words,outputFileName); 16 } 17 private static final int LIMIT = 1000; 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 int wordCount = index; 29 String[] words = new String[wordCount]; 30 for (int x = 0; x < wordCount; x = x + 1) { 31 words[x] = temp[x]; 32 } 33 return words; 34 } 35 private static void writeWordsToFile(String[] words, String outputFileName) throws FileNotFoundException { 36 PrintWriter printer = getPrintWriter(outputFileName); 37 for (int x = words.length - 1; x >= 0; x = x - 1) { 38 printer.println(words[x]); 39 } 40 printer.close(); 41 } 42 private static Scanner establishScanner(String inputFileName) throws FileNotFoundException { 43 String fullFileName = createFullFileName(inputFileName); 44 return new Scanner(new File(fullFileName)); 45 } 46 47 private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException { 48 String fullFileName = createFullFileName(outputFileName); 49 PrintWriter printer = new PrintWriter(fullFileName); 50 return printer; 51 } 52 53 private static String createFullFileName(String fileName) { 54 String separator = System.getProperty("file.separator"); 55 String home = System.getProperty("user.home"); 56 String path = home + separator + "desktop" + separator + "CS1Files" + separator + "data" + separator; 57 String fullFileName = path + fileName; 58 return fullFileName; 59 } 60 61 } 62 63