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 Smithereens { 11 public static void main(String[] args) throws FileNotFoundException, IOException { 12 String inputFileName = "Smithereens.text"; 13 String outputFileName = "SmithereensReversed"; 14 ArrayList<String> words = readWordsFromfile(inputFileName); 15 writeWordsToFile(words,outputFileName); 16 } 17 18 private static ArrayList<String> readWordsFromfile(String inputFileName) throws FileNotFoundException { 19 Scanner scanner = establishScanner(inputFileName); 20 ArrayList<String> words = new ArrayList<>(); 21 while (scanner.hasNext()) { 22 String word = scanner.next(); 23 words.add(word); 24 } 25 return words; 26 } 27 28 29 private static void writeWordsToFile(ArrayList<String> words, String outputFileName) throws IOException { 30 PrintWriter printer = getPrintWriter(outputFileName); 31 for (int x = words.size()-1; x>= 0; x = x-1) { 32 printer.println(words.get(x)); 33 } 34 printer.close(); 35 } 36 37 private static Scanner establishScanner(String inputFileName) throws FileNotFoundException { 38 String fullFileName = createFullFileName(inputFileName); 39 return new Scanner(new File(fullFileName)); 40 } 41 42 private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException { 43 String fullFileName = createFullFileName(outputFileName); 44 PrintWriter printer = new PrintWriter(fullFileName); 45 return printer; 46 } 47 private static String createFullFileName(String fileName) { 48 String separator = System.getProperty("file.separator"); 49 String home = System.getProperty("user.home"); 50 String path = home + separator + "CS1Files" + separator + "data" + separator; 51 String fullFileName = path + fileName; 52 return fullFileName; 53 } 54 55 56 57 58 59 60 61 62 } 63