forked from ellikale/FileWriterActivity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyFileWriter.java
More file actions
93 lines (82 loc) · 3.11 KB
/
MyFileWriter.java
File metadata and controls
93 lines (82 loc) · 3.11 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
import java.io.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
// for sha-356
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MyFileWriter {
public static void main(String[] args) throws IOException {
String fileName = "example.txt";
String data = "Talia!";
try (FileWriter writer = new FileWriter(fileName, false)) {
writer.write(data);
} catch (IOException e) {
e.printStackTrace();
}
String hash = hashFile(fileName);
System.out.println("SHA-256 of " + fileName + ": " + hash);
}
public static void generateHiddenFile(String password, String fileName) {
try {
Files.write(Paths.get(fileName), password.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
//hi!
}
// 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 {
// TODO: Implement this method
// Read the file at filePath and return its contents as a String
Path fileP = Paths.get(filePath);
return Files.readString(fileP);
}
public static String hashFile(String filePath){
try {
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
// reading the file in chunks of 1024 bytes at a time
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);
}
byte[] hashBytes = sha256.digest();
StringBuilder str = new StringBuilder();
// Converts the resulting hash to a hexadecimal string format
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;
}
}