/home/akc/NetBeansProjects/CS1/src/arrayplay/ReverseCopy.java |
1
2
3
4
5 package arrayplay;
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.Scanner;
12
13
14
15 @author
16
17 public class ReverseCopy {
18
19
20 @param args
21
22 public static void main(String[] args) throws FileNotFoundException, IOException{
23 String inputFileName = "HerMajesty.text";
24 String outputFileName = "HerMajestyReversed.text";
25 String [] words = readWordsFromFile (inputFileName);
26 writeWordsToFile (words, outputFileName);
27 }
28
29 private static final int LIMIT = 1000;
30
31 private static String[] readWordsFromFile(String inputFileName) throws FileNotFoundException{
32
33 Scanner scanner = establishScanner(inputFileName);
34
35 String[] temp = new String[LIMIT];
36 int index = 0;
37 while ( scanner.hasNext() ) {
38 String word = scanner.next();
39 temp[index] = word;
40 index = index + 1;
41 }
42 int wordCount = index;
43
44 String[] words = new String[wordCount];
45 for ( int x = 0; x < wordCount; x = x + 1 ){
46 words[x] = temp [x];
47 }
48
49 return words;
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