-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyFileWriter.java
More file actions
135 lines (96 loc) · 3.95 KB
/
MyFileWriter.java
File metadata and controls
135 lines (96 loc) · 3.95 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
import java.io.*;
import java.nio.file.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.nio.charset.StandardCharsets;
public class MyFileWriter {
// secret password file creator
public static void secretPassword() throws IOException {
// make secret password
BufferedWriter secretPassword = new BufferedWriter(new FileWriter(".whoNeeds2FA.txt"));
secretPassword.write("My genius, impossible to break password: password123");
// close bufferedWriter
secretPassword.close();
}
public static void confidentialPlans() throws IOException {
// get file paths
String directory = "/Users/scp/Desktop/School/12th Grade/Honors Topics/HTCS_Projects/FileWriterActivity/.topSecret";
// put the secret file in the .topSecret file
File secretFile = new File(directory, "plansForWorldDomination.dat");
// make secret plans
BufferedWriter secretPlans = new BufferedWriter(new FileWriter(secretFile));
secretPlans.write("Step 1: Eat breakfast \nStep 2: Discover the meaning of life \nStep 3: ??? \nStep 4: Profit");
// close bufferedWriter
secretPlans.close();
}
/**
* 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 {
// get the file
BufferedReader file = new BufferedReader(new FileReader(filePath));
String contents = "";
String line;
// read through the file
while ((line = file.readLine()) != null) {
contents += line;
}
// close the buffered reader
file.close();
// Read the file at filePath and return its contents as a String
return contents;
}
// to string method
public String toString() {
return "Hello, World!";
}
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 String hashFile(String filePath) {
// catch exceptions using try catch
try {
// check to see if the file exists
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException("This file " + filePath + " doesn't exist.");
}
// read the file
String fileContents = "";
// catch exceptions using try catch
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
fileContents += line;
}
}
// ceate sha-256 digest
MessageDigest digest = MessageDigest.getInstance("SHA-256");
// get the hash by counting bytes
byte[] hashBytes = digest.digest(fileContents.getBytes(StandardCharsets.UTF_8));
// convert hash to hex string
String hexCode = "";
for (int i = 0; i < hashBytes.length; i++) {
hexCode += String.format("%02x", hashBytes[i]);
}
return hexCode;
} catch (FileNotFoundException e) {
return "The file was not found. " + e.getMessage();
} catch (IOException e) {
return "The file could not be read. " + e.getMessage();
} catch (NoSuchAlgorithmException e) {
return "SHA-256 algorithm not available. " + e.getMessage();
}
}
}