/home/jfernan6/NetBeansProjects/CSX/src/arrayplay/ReverseCopy.java |
1
2
3
4
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
17
18 public class ReverseCopy {
19
20
21 @param args
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
34 Scanner scanner = establishScanner(inputFileName);
35
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
46 String[] words = new String[wordCount];
47 for (int x = 0; x < wordCount; x = x + 1) {
48 words[x] = temp[x];
49 }
50
51 return words;
52
53 }
54
55 private static void writeWordsToFile(String[] words, String outputFileName) throws IOException {
56
57 PrintWriter printer = getPrintWriter(outputFileName);
58
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
77
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