-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyFileWriter.java
More file actions
246 lines (215 loc) · 8.47 KB
/
MyFileWriter.java
File metadata and controls
246 lines (215 loc) · 8.47 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MyFileWriter {
private static void printFileSize(String... fileNames) {
long totalSize = 0;
for (String fileName : fileNames) {
File file = new File(fileName);
if (file.exists()) {
totalSize += file.length();
}
}
System.out.println("Total size of all files: " + totalSize + " bytes");
}
public static void main(String[] args) {
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();
}
writeSecretFile("secop fail :(");
printFileSize(".mysecret.txt");
writeSecretFile("yo");
printFileSize(".mysecret.txt");
writeSecretFile("The quick brown fox jumps over the lazy dog.");
printFileSize(".mysecret.txt");
writeInSecretFolder("We've been found!");
printFileSize(".undercoverfolder/coolstuff.txt");
try {
System.out.println(hashFile(".undercoverfolder/coolstuff.txt"));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
testHashFile();
testHashFileEmptyFiles();
testHashFileLargeFiles();
testHashFileSpecialChars();
testHashNonExistant();
}
public static String hashFile(String filePath) throws FileNotFoundException {
File f = new File(filePath);
String contents = "";
String line = "";
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(f))) {
line = bufferedReader.readLine();
while (line != null) {
contents += line;
line = bufferedReader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
throw new FileNotFoundException("File doesn't exist");
}
byte[] hashVal = new byte[0];
try {
hashVal = getSHA(contents);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
String hexaHash = toHexString(hashVal);
return hexaHash;
}
public static byte[] getSHA(String input) throws NoSuchAlgorithmException {
// Static getInstance method is called with hashing SHA
MessageDigest md = MessageDigest.getInstance("SHA-256");
// digest() method called
// to calculate message digest of an input
// and return array of byte
return md.digest(input.getBytes(StandardCharsets.UTF_8));
}
public static String toHexString(byte[] hash) {
// Convert byte array into signum representation
BigInteger number = new BigInteger(1, hash);
// Convert message digest into hex value
StringBuilder hexString = new StringBuilder(number.toString(16));
// Pad with leading zeros
while (hexString.length() < 64) {
hexString.insert(0, '0');
}
return hexString.toString();
}
// Writes a message to hidden file named ".mysecret.txt"
public static void writeSecretFile(String content) {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(".mysecret.txt"))) {
bufferedWriter.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
// Writes a message to non hidden file named "coolstuff.txt" in the hidden
// folder ".undercoverfolder"
public static void writeInSecretFolder(String content) {
File directory = new File(".undercoverfolder");
if (!directory.exists()) {
directory.mkdirs();
}
String filePath = directory.getPath() + "/coolstuff.txt";
System.out.println(filePath);
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath))) {
bufferedWriter.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
// Prints out the size of the given file path in bytes
private static void printFileSize(String fileName) {
File f = new File(fileName);
if (f.exists() && f.isFile()) {
long fileSize = f.length();
System.out.println("File size: " + fileSize + " bytes");
} else {
System.out.println("File does not exist");
}
}
// A to string method... for some reason
public String toString() {
return "I'm a file writer, bum bum ba dum bum bum";
}
public static void testHashFile() {
// Contains the message "Your fate is chosen..."
String testFilePath = "PredeterminedTest.txt";
String hashVal;
try {
hashVal = hashFile(testFilePath);
} catch (Exception e) {
return;
}
String expectedValue = "8d2aa27b229f4623d0b1509be943ac9da9b552d7e376624f8c32a7949915f8ec";
assert hashVal.equals(expectedValue) : "Hash was not performed correctly";
System.out.println("Hash was successful");
}
public static void testHashFileEmptyFiles() {
// An empty file
String testFilePath = "EmptyFile.txt";
String hashVal;
try {
hashVal = hashFile(testFilePath);
} catch (Exception e) {
return;
}
String expectedValue = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
assert hashVal.equals(expectedValue) : "Hash did not handle empty case correctly";
System.out.println("Empty file handled successfully");
}
public static void testHashFileLargeFiles() {
// Definitely not just the great gatsby text file with the newlines replace by spaces
String testFilePath = "LargeFile.txt";
String hashVal;
try {
hashVal = hashFile(testFilePath);
} catch (Exception e) {
return;
}
String expectedValue = "341776eb9826df4fbf232baff22e22c18d4ebe49eccccc2ebbfa2cebe524bf6b";
assert hashVal.equals(expectedValue) : "Hash did not handle empty case correctly";
System.out.println("Large file handled successfully");
}
public static void testHashFileSpecialChars() {
// Just 3 duck emojis
String testFilePath = "SpecialCharsFile.txt";
String hashVal;
try {
hashVal = hashFile(testFilePath);
} catch (Exception e) {
return;
}
String expectedValue = "20f498fed5aa49ada2aeae4a91c8acc06b8aabf3f1fd2bbf543a1e0ded065208";
assert hashVal.equals(expectedValue) : "Hash did not handle special character correctly";
System.out.println("Special characters handled successfully");
}
public static void testHashNonExistant() {
String testFilePath = "NonexistantFile.txt";
try {
String hashVal = hashFile(testFilePath);
System.out.println("Error not thrown properly, got the hash: " + hashVal);
} catch (Exception e) {
System.out.println("Error thrown properly");
}
}
}