1 package Arrayplay; 2 // program to copy song lyrics in reverse 3 4 import javax.swing.*; 5 import java.io.File; 6 import java.io.FileNotFoundException; 7 import java.io.IOException; 8 import java.io.PrintWriter; 9 import java.util.Scanner; 10 11 public class ReverseCopy { 12 public static void main(String[] args) throws FileNotFoundException, IOException { 13 String inputFileName = "SongLyrics"; 14 String outputFileName = "SongLyricsReversed"; 15 String[] words = readWordsFromFile(inputFileName); 16 writeWordsToFile(words,outputFileName); 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 void writeWordsToFile(String[] words, String outputFileName) throws IOException { 38 PrintWriter printer = getPrintWriter(outputFileName); 39 for ( int x = words.length - 1; x >= 0; x = x - 1) { 40 printer.println(words[x]); 41 } 42 printer.close(); 43 } 44 45 private static Scanner establishScanner(String inputFileName) throws FileNotFoundException { 46 String fullFileName = createFullFileName(inputFileName); 47 return new Scanner(new File(fullFileName)); 48 49 } 50 51 private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException { 52 String fullFileName = createFullFileName(outputFileName); 53 PrintWriter printer = new PrintWriter(fullFileName); 54 return printer; 55 } 56 private static String createFullFileName(String fileName) { 57 String separator = System.getProperty("file.separator"); 58 String home = System.getProperty("user.home"); 59 String path = home + separator + "CS1Files" + separator + "data" + separator; 60 String fullFileName = path + fileName; 61 return fullFileName; 62 63 } 64 } 65