1 package arrayplay; 2 import java.io.File; 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.io.PrintWriter; 6 import java.util.ArrayList; 7 import java.util.Scanner; 8 public class ReverseCopy 9 { 10 public static void main(String[] args) throws FileNotFoundException, IOException 11 { 12 String inputFileName = "lyrics.text"; 13 String outputFileName = "lyricsreversed.text"; 14 ArrayList<String> words = readWordsFromFiles(inputFileName); 15 writeWordsToFiles(words, outputFileName); 16 } 17 private static ArrayList<String> readWordsFromFiles(String inputFileName) throws FileNotFoundException 18 { 19 Scanner scanner = establishScanner(inputFileName); 20 ArrayList<String> words = new ArrayList<>(); 21 while (scanner.hasNext()) 22 { 23 String word = scanner.next(); 24 words.add(word); 25 } 26 return words; 27 } 28 private static void writeWordsToFiles(ArrayList<String> words, String outputFileName) throws IOException 29 { 30 PrintWriter printer = getPrintWriter(outputFileName); 31 for (int x = words.size() - 1; x >= 0; x = x - 1) 32 { 33 printer.println(words.get(x)); 34 } 35 printer.close(); 36 } 37 private static Scanner establishScanner(String inputFileName) throws FileNotFoundException 38 { 39 String fullFileName = createFullFileName(inputFileName); 40 return new Scanner(new File(fullFileName)); 41 } 42 private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException 43 { 44 String fullFileName = createFullFileName(outputFileName); 45 PrintWriter printer = new PrintWriter(fullFileName); 46 return printer; 47 } 48 private static String createFullFileName(String fileName) 49 { 50 String separator = System.getProperty("file.separator"); 51 String home = System.getProperty("user.home"); 52 String path = home + separator + "CS1Files" + separator + "data" + separator; 53 String fullFileName = path + fileName; 54 return fullFileName; 55 } 56 57 }