/home/jfernan6/NetBeansProjects/CSX/src/arrayplay/ReverseCopy.java
 1 /*
 2  * To change this license header, choose License Headers in Project Properties.
 3  * To change this template file, choose Tools | Templates
 4  * and open the template in the editor.
 5  */
 6 package arrayplay;
 7 
 8 import java.io.File;
 9 import java.io.FileNotFoundException;
10 import java.io.IOException;
11 import java.io.PrintWriter;
12 import java.util.Scanner;
13 
14 /**
15  *
16  * @author jfernan6
17  */
18 public class ReverseCopy {
19 
20     /**
21      * @param args the command line arguments
22      */
23     public static void main(String[] args) throws FileNotFoundException, IOException {
24         String inputFileName = "VibeRatings.text";
25         String outputFileName = "ForeverYoungReversed.text";
26         String[] words = readWordsFromFile(inputFileName);
27         writeWordsToFile(words, outputFileName);
28     }
29 
30     private static final int LIMIT = 1000;
31 
32     private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException {
33         //EQUATE A SCANNER WITH THE INPUT FILE
34         Scanner scanner = establishScanner(inputFileName);
35         //READ THE WORDS FROM THE FILE INTO AN OVERSIZED ARRAY
36         String[] temp = new String[LIMIT];
37         int index = 0;
38         while (scanner.hasNext()) {
39             String word = scanner.next();
40             temp[index] = word;
41             index = index + 1;
42         }
43 
44         int wordCount = index;
45         //TRANSFER THE WORDS TO A PERFECTLY SIZED ARRAY
46         String[] words = new String[wordCount];
47         for (int x = 0; x < wordCount; x = x + 1) {
48             words[x] = temp[x];
49         }
50         // RETURN THE WORDS
51         return words;
52 
53     }
54 
55     private static void writeWordsToFile(String[] words, String outputFileName) throws IOException {
56         // EQUATE A PRINTER WITH AN OUTPUT FILE
57         PrintWriter printer = getPrintWriter(outputFileName);
58         //PRINT THE WORDS TO THE FILE
59         for (int x = words.length - 1; x >= 0; x = x - 1) {
60             printer.println(words[x]);
61         }
62         printer.close();
63     }
64 
65     private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
66         String fullFileName = createFullFileName(inputFileName);
67         return new Scanner(new File(fullFileName));
68     }
69 
70     private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
71         String fullFileName = createFullFileName(outputFileName);
72         PrintWriter printer = new PrintWriter(fullFileName);
73         return printer;
74     }
75 
76     //CREATER THE FULL FILE NAME FOR A SIMPLE NAME, ASSUMING THAT IT WILL BE 
77     //FOUND IN THE CS1FILES/DATA SUBDIRECTORY OF THE USER'S HOME DIRECTORY.
78     private static String createFullFileName(String fileName) {
79         String separator = System.getProperty("file.separator");
80         String home = System.getProperty("user.home");
81         String path = home + separator + "CS1Files" + separator + "data" + separator;
82         String fullFileName = path + fileName;
83         return fullFileName;
84     }
85 
86 }
87