1 /* 2 * Program featuring straight up arrays and file IO to read and reverse copy a lyric. 3 */ 4 5 package arrayplay; 6 7 import java.io.File; 8 import java.io.FileNotFoundException; 9 import java.io.IOException; 10 import java.io.PrintWriter; 11 import java.util.Scanner; 12 13 public class ReverseCopy { 14 15 public static void main(String[] args) throws IOException { 16 17 String inputFileName = "SweetDreams.txt"; 18 String outputFileName = "SweetDreamsReversed.txt"; 19 String[] words = readWordsFromFile(inputFileName); 20 writeWordsToFile(words,outputFileName); 21 22 } 23 24 private static final int LIMIT = 1000; 25 26 private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException { 27 28 Scanner scanner = establishedScanner(inputFileName); 29 String[] temp = new String[LIMIT]; 30 int index = 0; 31 while (scanner.hasNext() ) { 32 String word = scanner.next(); 33 temp[index] = word; 34 index = index + 1; 35 } 36 37 int wordCount = index; 38 String[] words = new String[wordCount]; 39 for (int x = 0; x < wordCount; x = x + 1 ) { 40 words[x] = temp[x]; 41 } 42 43 return words; 44 45 } 46 47 private static void writeWordsToFile(String[] words, String outputFileName) throws IOException { 48 49 PrintWriter printer = getPrintWriter(outputFileName); 50 for (int x = words.length-1; x >= 0; x = x -1) { 51 printer.println(words[x]); 52 } 53 54 printer.close(); 55 56 } 57 58 private static Scanner establishedScanner(String inputFileName) throws FileNotFoundException { 59 String fullFileName = createFullFileName(inputFileName); 60 return new Scanner(new File(fullFileName)); 61 } 62 63 private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException { 64 String fullFileName = createFullFileName(outputFileName); 65 PrintWriter printer = new PrintWriter(fullFileName); 66 return printer; 67 } 68 69 private static String createFullFileName(String fileName) { 70 71 String separator = System.getProperty("file.separator"); 72 String home = System.getProperty("user.home"); 73 String path = home + separator + "CS1Files" + separator + "data" + separator; 74 String fullFileName = path + fileName; 75 return fullFileName; 76 77 } 78 79 } 80