/home/ssingh6/NetBeansProjects/CS1/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 = "Attention.txt";
25 String outputFileName = "AttentionReversed.txt";
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 int wordCount = index;
44
45 String[] words = new String[wordCount];
46 for ( int x = 0; x < wordCount; x = x + 1 ) {
47 words[x] = temp[x];
48 }
49
50 return words;
51 }
52
53 private static void writeWordstoFile(String[] words, String outputFileName) throws IOException {
54
55 PrintWriter printer = getPrintWriter(outputFileName);
56
57 for ( int x = words.length-1; x >= 0; x = x - 1) {
58 printer.println(words[x]);
59 }
60 printer.close();
61 }
62
63 private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
64 String fullFileName = createFullFileName(inputFileName);
65 return new Scanner(new File(fullFileName));
66 }
67
68 private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
69 String fullFileName = createFullFileName(outputFileName);
70 PrintWriter printer = new PrintWriter(fullFileName);
71 return printer;
72 }
73
74
75
76
77 private static String createFullFileName(String fileName) {
78 String separator = System.getProperty("file.separator");
79 String home = System.getProperty("user.home");
80 String path = home + separator + "CS1Files" + separator + "data" + separator;
81 String fullFileName = path + fileName;
82 return fullFileName;
83 }
84
85 }
86
87