/home/rkanin/NetBeansProjects/CS1/src/arraylistplay/ReverseCopy.java
 1 /*
 2 2*ThisprogramfeaturesanArrayListtodoitsreversecopythingfromonefiletoanother.
 3 3*/
 4 
 5 package arraylistplay;
 6 
 7 import java.io.File;
 8 import java.io.FileNotFoundException;
 9 import java.io.IOException;
10 import java.io.PrintWriter;
11 import java.util.ArrayList;
12 import java.util.Scanner;
13 
14 
15 
16 
17   /**
18 
19     *
20     * @author blue
21     */
22    public class ReverseCopy {
23 
24     
25        /**
26         * @param args the command line arguments
27         */
28        public static void main(String[] args) throws FileNotFoundException, IOException {
29                    String inputFileName = "ParadiseRK.text";
30                    String outputFileName = "ParadiseRKreversed.text";
31                    ArrayList<String> words = readWordsFromFile(inputFileName);
32                    writeWordsToFile(words, outputFileName);
33                }
34 
35     
36        private static ArrayList<String> readWordsFromFile(String inputFileName) throws FileNotFoundException, IOException {
37            // Equate a scanner with the input file
38            Scanner scanner = establishScanner(inputFileName);
39                // Read the words from the file into a dynamically growing ArrayList
40                ArrayList<String> words = new ArrayList<>();
41 
42                while ( scanner.hasNext () ) {
43                String word = scanner.next();
44                        words.add(word);
45                    }
46                // Return the words
47            return words ;
48 
49        }
50 
51        private static void writeWordsToFile(ArrayList<String> words, String outputFileName) throws IOException {
52            // Equate a printer with an output file
53            PrintWriter printer = getPrintWriter(outputFileName);
54            // Print the words to the file
55            for ( int x = words.size() - 1; x >= 0;x=x-1){
56                printer.println(words.get(x));
57            }
58            printer.close();
59        }
60 
61        private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
62            String fullFileName = createFullFileName(inputFileName);
63            return new Scanner(new File(fullFileName));
64        }
65 
66        private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
67            String fullFileName = createFullFileName(outputFileName);
68            PrintWriter printer = new PrintWriter(fullFileName);
69            return printer;
70        }
71 
72        // Create the full file name for a simple file name, assuming that it will be
73        // found in the CS1Files/data subdirectory of the user’s home directory.
74        private static String createFullFileName(String fileName) {
75            String separator = System.getProperty("file.separator");
76            String home = System.getProperty("user.home");
77 
78            String path = home + separator + "CS1Files" + separator + "data" + separator;
79            String fullFileName = path + fileName;
80            return fullFileName;
81        }
82    }
83 
84 
85