/home/ssingh6/NetBeansProjects/CS1/src/arraylistplay/ReverseCopy.java |
1
2
3
4
5
6 package arraylistplay;
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.ArrayList;
13 import java.util.Scanner;
14
15
16
17 @author
18
19 public class ReverseCopy {
20
21
22 @param args
23
24 public static void main(String[] args) throws FileNotFoundException, IOException {
25 String inputFileName = "HowLong.txt";
26 String outputFileName = "HowLongReversed.txt";
27 ArrayList<String> words = readWordsFromFile(inputFileName);
28 writeWordstoFile(words,outputFileName);
29 }
30
31 private static ArrayList<String> readWordsFromFile(String inputFileName) throws FileNotFoundException {
32
33 Scanner scanner = establishScanner(inputFileName);
34
35 ArrayList<String> words = new ArrayList<>();
36 while ( scanner.hasNext() ) {
37 String word = scanner.next();
38 words.add(word);
39 }
40
41 return words;
42 }
43
44 private static void writeWordstoFile(ArrayList<String> words, String outputFileName) throws IOException {
45
46 PrintWriter printer = getPrintWriter(outputFileName);
47
48 for ( int x = words.size()-1; x >= 0; x = x - 1) {
49 printer.println(words.get(x));
50 }
51 printer.close();
52 }
53
54 private static Scanner establishScanner(String inputFileName) throws FileNotFoundException {
55 String fullFileName = createFullFileName(inputFileName);
56 return new Scanner(new File(fullFileName));
57 }
58
59 private static PrintWriter getPrintWriter(String outputFileName) throws FileNotFoundException {
60 String fullFileName = createFullFileName(outputFileName);
61 PrintWriter printer = new PrintWriter(fullFileName);
62 return printer;
63 }
64
65
66
67
68 private static String createFullFileName(String fileName) {
69 String separator = System.getProperty("file.separator");
70 String home = System.getProperty("user.home");
71 String path = home + separator + "CS1Files" + separator + "data" + separator;
72 String fullFileName = path + fileName;
73 return fullFileName;
74 }
75
76 }
77
78