1 package arraylistplay; 2 // program to copy song lyrics in reverse 3 import java.util.ArrayList; 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 = "SongLyricsLowRider.text"; 14 String outputFileName = "SongLyricsLowRiderReversed"; 15 ArrayList<String> words = readWordsFromFile(inputFileName); 16 writeWordsToFile(words ,outputFileName); 17 } 18 private static final int LIMIT = 1000; 19 20 private static ArrayList<String> readWordsFromFile(String inputFileName) throws FileNotFoundException { 21 Scanner scanner = establishScanner(inputFileName); 22 ArrayList<String> words = new ArrayList<>(); 23 24 while (scanner.hasNext() ) { 25 String word = scanner.next(); 26 words.add(word); 27 } 28 return words; 29 } 30 31 private static void writeWordsToFile(ArrayList<String> words, String outputFileName) throws IOException { 32 PrintWriter printer = getPrintWriter(outputFileName); 33 for ( int x = words.size() - 1; x >= 0; x = x - 1) { 34 printer.println(words.get(x)); 35 } 36 printer.close(); 37 } 38 39 private static Scanner establishScanner(String inputFileName) throws FileNotFoundException { 40 String fullFileName = createFullFileName(inputFileName); 41 return new Scanner(new File(fullFileName)); 42 43 } 44 45 private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException { 46 String fullFileName = createFullFileName(outputFileName); 47 PrintWriter printer = new PrintWriter(fullFileName); 48 return printer; 49 } 50 private static String createFullFileName(String fileName) { 51 String separator = System.getProperty("file.separator"); 52 String home = System.getProperty("user.home"); 53 String path = home + separator + "CS1Files" + separator + "data" + separator; 54 String fullFileName = path + fileName; 55 return fullFileName; 56 57 } 58 }