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 = "SeizeTheDay.text"; 13 String outputFileName = "SeizeTheDayReverse.text"; 14 String[] words = readWordsFromFile(inputFileName); 15 writeWordsToFile(words,outputFileName); 16 } 17 18 private static final int LIMIT = 1000; 19 20 private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException { 21 Scanner scanner = establishScanner(inputFileName); 22 String[] temp = new String[LIMIT]; 23 int index = 0; 24 while(scanner.hasNext()) { 25 String word = scanner.next(); 26 temp[index] = word; 27 index = index + 1; 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 Scanner establishScanner(String inputFileName) throws FileNotFoundException { 38 String fullFileName = createFullFileName(inputFileName); 39 return new Scanner(new File(fullFileName)); 40 } 41 42 private static String createFullFileName(String fileName) { 43 String separator = System.getProperty("file.separator"); 44 String home = System.getProperty("user.home"); 45 String path = home + separator + "CS1Files" + separator + "data" + separator; 46 String fullFileName = path + fileName; 47 return fullFileName; 48 } 49 50 private static void writeWordsToFile(String[] words, String outputFileName) throws IOException { 51 PrintWriter printer = getPrinterWriter(outputFileName); 52 for ( int x = words.length-1; x >= 0; x = x - 1){ 53 printer.println(words[x]); 54 } 55 printer.close(); 56 } 57 58 private static PrintWriter getPrinterWriter(String outputFileName) throws FileNotFoundException { 59 String fullFileName = createFullFileName(outputFileName); 60 PrintWriter printer = new PrintWriter(fullFileName); 61 return printer; 62 } 63 } 64