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