-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileHandler.java
73 lines (62 loc) · 2.39 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
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
/*
FileHandler: Handles all writing and reading, to and from files.
*/
import java.io.*;
import java.math.BigInteger;
public class FileHandler {
private File keyFilePath;
private byte[] text;
private final Keys keys = new Keys();
/*
Method: checkIfFilePathExists
Purpose: Receive absolute file path from user and
return true if path exists - false otherwise.
*/
public void checkIfFilePathSExists(String[] args) {
File sourceFilePath = new File(args[0]);
File destinationFilePath = new File(args[1]);
this.keyFilePath = new File(args[2]);
if (sourceFilePath.exists() & destinationFilePath.exists() & keyFilePath.exists()) // Check if files exist
assert true; // If they do exist do nothing
else { // Else file does not exist
System.out.println("[!] The file path for one or more of your files does not exist. Please provide the absolute path for each file");
System.exit(0); // Terminate program
}
}
/*
Method: checkIfFilePathExists
Purpose: Receive absolute file path from user and
return true if path exists - false otherwise.
*/
public void checkIfKeyFilePathExists(String[] arg) {
this.keyFilePath = new File(arg[0]);
if (keyFilePath.exists()) // Check if file exists
assert true; // If it does do nothing
else { // Else file does not exist
System.out.println("The file path " + keyFilePath + " does not exist");
System.exit(0); // Terminate program
}
}
/*
Method: writeToFile
Purpose: Write byte stream to file
*/
public void writeToFile(String filePath, BigInteger byteStream) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
fileOutputStream.write(byteStream.toByteArray());
fileOutputStream.close();
}
/*
Method: getText
Purpose: Read text from file as byte array
*/
public void getText(String sourceFilePath) throws IOException {
this.text = keys.inputStreamByteArray(new FileInputStream(sourceFilePath));
}
public File getKeyFilePath() {
return keyFilePath;
}
public byte[] getText() {
return text;
}
}