-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyFileWriter.java
More file actions
239 lines (184 loc) · 7.96 KB
/
MyFileWriter.java
File metadata and controls
239 lines (184 loc) · 7.96 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
import java.io.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MyFileWriter {
public static void main(String[] args) throws IOException {
// generateRegularFile();
// generateHiddenFile();
// String data = "Hello, World!";
// String fileName1 = "example.txt";
// String fileName2 = "example2.txt";
// String fileName3 = "example3.txt";
// String fileName4 = "example4.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();
// }
// printFileSize("example5.txt");
// try {
// System.out.println(stringify("example5.txt"));
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
File file1 = new File("file1.txt");
File file2 = new File("file2.txt");
File file3 = new File("file3.txt");
Files.writeString(file1.toPath(), "Hellur");
Files.writeString(file2.toPath(), "My name is ellikakaka");
Files.writeString(file3.toPath(), "bababooey went to topics class and cried");
printFileSize("file1.txt", "file2.txt", "file3.txt", "smush.txt");
String h1 = hashFile("file1.txt");
String h2 = hashFile("file2.txt");
System.out.println("Hash of file1.txt: " + h1);
System.out.println("Hash of file2.txt: " + h2);
testHashFileEmptyFiles();
testHashFileLargeFiles();
testHashFileSpecialChars();
testHashFileNonExistent();
}
public static void generateHiddenFile() {
// 5. Using Files (java.nio.file)
try {
String data = "pookie";
Files.write(Paths.get(".secrtpswd.txt"), data.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void generateRegularFile() {
// 5. Using Files (java.nio.file)
try {
String data = "confidential info";
Files.write(Paths.get(".topsecret/confidentiallll.txt"), data.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
// Calculate and print the file size using the File class
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");
}
/**
* Reads a text file and returns its contents as a string.
*
* @param filePath the path to the file
* @return the contents of the file as a string
* @throws IOException if an I/O error occurs
*/
public static String stringify(String filePath) throws IOException {
Path filePathh = Paths.get(filePath);
String content = Files.readString(filePathh);
return content;
}
public static String hashFile(String filePath) {
try {
// info on message digest: https://docs.oracle.com/javase/8/docs/api/java/security/MessageDigest.html
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
// Read file in chunks of 1024 bytes using fileinputstream: https://docs.oracle.com/javase/8/docs/api/java/io/FileInputStream.html and a buffer byte array: https://docs.oracle.com/javase/tutorial/essential/io/file.html
try (InputStream fileInputS = new FileInputStream(filePath)) {
byte[] temporary = new byte[1024];
int numberBytes = fileInputS.read(temporary);
while (numberBytes != -1) {
sha256.update(temporary, 0, numberBytes);
numberBytes = fileInputS.read(temporary);
}
// finalizing hash
byte[] hashBytes = sha256.digest();
// convert bytes to readable hex string: https://www.baeldung.com/sha-256-hashing-java
StringBuilder str = new StringBuilder();
for (byte b : hashBytes) {
String hex = String.format("%02x", b);
str.append(hex);
}
return str.toString();
}
} catch (FileNotFoundException e) {
System.err.println("Error: File not found -> " + filePath);
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
} catch (NoSuchAlgorithmException e) {
System.err.println("Error: SHA-256 algorithm not available.");
}
return null;
}
// empty file test
// expected hash: e3b0c442... (the known empty sha256)
// got the same when I ran it---> pass
public static void testHashFileEmptyFiles() throws IOException {
// creates a 0-byte empty file
Path emptyFile = Paths.get("emptyTest.txt");
Files.write(emptyFile, new byte[0]);
// hashes the empty file
String hash = hashFile("emptyTest.txt");
// known SHA-256 hash of an empty file (verified online)
String expected = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
System.out.println("Empty file hash: " + hash);
System.out.println("Matches expected? " + hash.equals(expected));
}
// large file test
// made a file with 100k lines, hash ran fine, didn’t crash ; pass
public static void testHashFileLargeFiles() throws IOException {
// making a LARGEE test file
Path largeFile = Paths.get("largeTest.txt");
StringBuilder stringBuild = new StringBuilder();
for (int i = 0; i < 100000; i++) {
stringBuild.append("bababoey.\n");
}
Files.writeString(largeFile, stringBuild.toString());
// Hash the large file
String hash = hashFile("largeTest.txt");
System.out.println("Hash of large file: " + hash);
}
// special chars test
// file had chinese, arabic, hebrew, emoji ; hash came out length 64 → pass
public static void testHashFileSpecialChars() throws IOException {
// making a file with with non-ASCII content
Path specialFile = Paths.get("specialTest.txt");
Files.writeString(specialFile, "你好世界 🚀 مرحبا بالعالمשלום עולם");
// hashing the file
String hash = hashFile("specialTest.txt");
System.out.println("Special chars file hash: " + hash);
System.out.println("Pass? " + (hash != null && hash.length() == 64));
}
// non-existent file test
// tried to hash a file that doesn’t exist, got error + null ; pass
public static void testHashFileNonExistent() {
String result = hashFile("no_such_file.txt");
// should get FileNotFoundException, so should return
System.out.println("Non-existent file test: " + (result == null));
}
}