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