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 public static void main (String[] args) throws FileNotFoundException, IOException{ 11 String inputFileName = "LaVieEnRose.text"; 12 String outputFileName = "LaVieEnRoseReversed.text"; 13 String[] words = readWordsFromFile(inputFileName); 14 writeWordsFromFile(words, outputFileName); 15 } 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 36 private static void writeWordsFromFile(String[] words, String outputFileName) throws IOException { 37 PrintWriter printer = getPrintWriter(outputFileName); 38 for ( int x = words.length-1; x >=0; x= x-1){ 39 printer.println(words[x]); 40 } 41 printer.close(); 42 } 43 private static Scanner establishScanner(String inputFileName) throws FileNotFoundException { 44 String fullFileName = createFullFileName(inputFileName); 45 return new Scanner(new File(fullFileName)); 46 } 47 48 private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException { 49 String fullFileName = createFullFileName(outputFileName); 50 PrintWriter printer = new PrintWriter(fullFileName); 51 return printer; 52 } 53 54 private static String createFullFileName(String fileName) { 55 String separator = System.getProperty("file.separator"); 56 String home = System.getProperty("user.home"); 57 String path = home + separator + "CS1Files" + separator + "data" + separator; 58 String fullFileName = path + fileName; 59 return fullFileName; 60 } 61 } 62