-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileHandler.java
45 lines (34 loc) · 1.08 KB
/
FileHandler.java
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
package ie.gmit.dip;
import java.io.*;
import java.net.URL;
public class FileHandler {// Parses files, handles input/output
private RailFenceCypher cypher;
private FileWriter fw = null;
public FileHandler(RailFenceCypher c) {
cypher = c;
}
public String getFileLocation() {
return getFileLocation();
}
public void FileParser() throws IOException {
/*
* Exceptions thrown by methods, two types checked and unchecked - Checked in this
* case - the compiler forces the developer to address
*/
parse(new FileInputStream(new File("file.txt")), true);
}
public void UrlParser() throws IOException {
parse(new URL("http://...").openStream(), true);
}
// Polymorphism - can read in any input stream
public void parse(InputStream in, boolean encrypt) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
String text = encrypt ? cypher.encrypt(line) : cypher.decrypt(line);
fw.write(text);
br.close();
fw.close();
}
}
}