-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleIO.java
More file actions
119 lines (108 loc) · 3.45 KB
/
SimpleIO.java
File metadata and controls
119 lines (108 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package simpleIO;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.swing.JFileChooser;
/**
* Provides a simple package for reading and writing files.
*
* @author Dave Matuszek
* @version Mar 27, 2009
*/
public class SimpleIO {
static String fileName;
/**
* Reads in lines from a user-chosen file.
*
* @return The list of lines.
* @throws IOException In the event of an error.
*/
public static ArrayList<String> load() throws IOException {
ArrayList<String> lines = null;
BufferedReader reader;
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Load which file?");
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
if (file != null) {
fileName = file.getCanonicalPath();
reader =
new BufferedReader(new FileReader(fileName));
lines = new ArrayList<String>();
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
reader.close();
return lines;
}
}
return lines;
}
/**
* Does the actual work of saving lines to a file.
* @param lines The lines to be saved.
* @throws IOException In the event of an error.
*/
public static void save(ArrayList<String> lines) throws IOException {
if (fileName == null) {
saveAs(lines);
} else {
writeTo(fileName, lines);
}
}
/**
* Saves lines to a user-specified file.
*
* @param lines The lines to be saved.
* @throws IOException In the event of an error.
*/
public static void saveAs(ArrayList<String> lines) throws IOException {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Save file as? ");
int result = chooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
if (file != null) {
fileName = file.getCanonicalPath();
writeTo(fileName, lines);
}
}
}
/**
* Saves lines to the same file from which they were
* previously read.
*
* @param outputFileName The name of the file.
* @param lines The lines to be saved.
* @throws IOException If no file was previously read, or
* the file could not be written.
*/
private static void writeTo(String outputFileName, ArrayList<String> lines)
throws IOException {
PrintWriter writer;
if (outputFileName == null) {
throw new IOException("No file has been loaded.");
}
writer = new PrintWriter(new FileOutputStream(outputFileName));
for (String line : lines) {
writer.println(line);
}
writer.close();
}
/**
* FOR TESTING PURPOSES ONLY.
* @param args Unused.
* @throws IOException In the event of an error.
*/
public static void main(String[] args) throws IOException {
ArrayList<String> lines;
lines = load();
saveAs(lines);
}
}