-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyFileWriterTester.java
More file actions
136 lines (97 loc) · 4.34 KB
/
MyFileWriterTester.java
File metadata and controls
136 lines (97 loc) · 4.34 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
public class MyFileWriterTester {
// Tester methods
public static String testHashFileEmptyFiles() throws IOException {
BufferedWriter emptyFile = new BufferedWriter(new FileWriter("emptyFile.txt"));
// close bufferedWriter
emptyFile.close();
// test the hash method
return MyFileWriter.hashFile("emptyFile.txt");
}
public static String testHashFileLargeFiles(String filePath) {
// test the hash method
return MyFileWriter.hashFile(filePath);
}
public static String testHashFileSpecialChars(String filePath) {
// test the hash method
return MyFileWriter.hashFile(filePath);
}
public static String testHashFileNonExistent(String filePath) {
// test the hash method
return MyFileWriter.hashFile(filePath);
}
public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
String data = "Hello, World!";
String fileName1 = "example.txt";
String fileName2 = "example2.txt";
String fileName3 = "example3.txt";
String fileName4 = "example4.txt";
String fileName5 = "example5.txt";
// 1. Using FileWriter
try (FileWriter writer = new FileWriter(fileName1)) {
writer.write(data);
} catch (IOException e) {
e.printStackTrace();
}
// 2. Using BufferedWriter
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName2))) {
bufferedWriter.write(data);
} catch (IOException e) {
e.printStackTrace();
}
// 3. Using FileOutputStream
try (FileOutputStream outputStream = new FileOutputStream(fileName3)) {
outputStream.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
// 4. Using BufferedOutputStream
try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(fileName4))) {
bufferedOutputStream.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
// 5. Using Files (java.nio.file)
try {
Files.write(Paths.get(fileName5), data.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
// SEE IF THE SECRET PLAN STUFF WORKS
MyFileWriter.secretPassword();
MyFileWriter.confidentialPlans();
System.out.println();
// stringify()
System.out.println(MyFileWriter.stringify("example.txt"));
System.out.println(MyFileWriter.stringify(".whoNeeds2FA.txt"));
System.out.println();
// hashFile
System.out.println("==hashFile()==");
System.out.println(MyFileWriter.hashFile(fileName1)); // dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f
System.out.println();
System.out.println("testHashFileEmptyFiles(): ");
System.out.println(testHashFileEmptyFiles()); // should print out: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
System.out.println(testHashFileEmptyFiles()); // should print same as above
System.out.println();
System.out.println("testHashFileLargeFiles(): ");
System.out.println(testHashFileLargeFiles("theGreatGatsby.txt")); // no error
System.out.println(testHashFileLargeFiles("theFellowshipOfTheRing.txt")); // no error
System.out.println();
System.out.println("testHashFileSpecialChars(): ");
System.out.println(testHashFileSpecialChars("nonAscii1.txt")); // no error
System.out.println(testHashFileSpecialChars("nonAscii2.txt")); // no error
System.out.println();
System.out.println("testHashFileNonExistent(): ");
System.out.println(testHashFileNonExistent("HelloWorld.txt")); // FileNotFoundException
System.out.println(testHashFileNonExistent("idkWhatToPutHere.txt")); // FileNotFoundException
System.out.println();
}
}