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.ArrayList; 8 import java.util.Scanner; 9 10 public class ReverseCopy { 11 12 public static void main(String[] args) throws FileNotFoundException, IOException { 13 String inputFileName = "Lyricstwo.txt"; 14 String outputFileName = "Lyricstworeversed.txt"; 15 ArrayList<String> words = readWordsFromFile(inputFileName); 16 writeWordsToFile(words, outputFileName); 17 } 18 19 private static ArrayList<String> readWordsFromFile(String inputFileName) throws FileNotFoundException { 20 Scanner scan = establishScanner(inputFileName); 21 ArrayList<String> words = new ArrayList<>(); 22 while (scan.hasNext()) { 23 String word = scan.next(); 24 words.add(word); 25 } 26 return words; 27 } 28 private static void writeWordsToFile(ArrayList<String> words, String outputFileName)throws IOException{ 29 PrintWriter print= getPrintWriter(outputFileName); 30 for (int x = words.size()-1; x >= 0; x = x - 1){ 31 print.println(words.get(x)); 32 } 33 print.close(); 34 } 35 private static Scanner establishScanner(String inputFileName)throws FileNotFoundException{ 36 String fullFileName = createFullFileName(inputFileName); 37 return new Scanner(new File(fullFileName)); 38 } 39 private static PrintWriter getPrintWriter(String outputFileName)throws FileNotFoundException{ 40 String fullFileName = createFullFileName(outputFileName); 41 PrintWriter printer = new PrintWriter(fullFileName); 42 return printer; 43 } 44 45 private static String createFullFileName(String fileName) { 46 String separator = System.getProperty("file.separator"); 47 String home = System.getProperty("user.home"); 48 String path = home + separator + "public_html" + separator + "data" + separator; 49 String fullFileName = path + fileName; 50 return fullFileName; 51 } 52 } 53