/home/evankemp/NetBeansProjects/CS1/src/arrayplay/ReverseCopy.java |
1
2
3
4 package arrayplay;
5
6 import java.io.File;
7 import java.io.FileNotFoundException;
8 import java.io.IOException;
9 import java.io.PrintWriter;
10 import java.util.Scanner;
11
12
13
14 @author
15
16 public class ReverseCopy {
17
18
19 @param args
20
21 public static void main(String[] args) throws FileNotFoundException, IOException {
22 String inputFileName = "AuClairDeLaLune.text";
23 String outputFileName = "AuClairDeLaLuneReversed.text";
24 String[] words = readWordsFromFile(inputFileName);
25 writeWordsToFile(words, outputFileName);
26 }
27
28 private static final int LIMIT = 1000;
29
30 private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException {
31
32 Scanner scanner = establishScanner(inputFileName);
33
34 String[] temp = new String[LIMIT];
35 int index= 0;
36 while (scanner.hasNext()) {
37 String word = scanner.next();
38 temp[index] = word;
39 index = index + 1;
40 }
41 int wordCount = index;
42
43 String[] words = new String[wordCount];
44 for (int x = 0; x < wordCount; x=x+1) {
45 words[x] = temp[x];
46 }
47
48 return words;
49
50 }
51
52 private static void writeWordsToFile(String[] words, String outputFileName) throws IOException{
53
54 PrintWriter printer = getPrintWriter(outputFileName);
55
56 for (int x =words.length-1; x>= 0; x=x-1) {
57 printer.println(words[x]);
58 }
59 printer.close();
60 }
61
62 private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
63 String fullFileName = createFullFileName(inputFileName);
64 return new Scanner(new File(fullFileName));
65 }
66
67 private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
68 String fullFileName = createFullFileName(outputFileName);
69 PrintWriter printer = new PrintWriter(fullFileName);
70 return printer;
71 }
72
73
74
75 private static String createFullFileName(String fileName) {
76 String separator = System.getProperty("file.separator");
77 String home = System.getProperty("user.home");
78 String path = home + separator + "CS1Files" + separator + "data" + separator;
79 String fullFileName = path + fileName;
80 return fullFileName;
81 }
82
83 }
84