-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyFileWriter.java
More file actions
124 lines (103 loc) · 4.27 KB
/
MyFileWriter.java
File metadata and controls
124 lines (103 loc) · 4.27 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
import java.io.*;
import java.nio.file.*;
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 data = "Hello, World!";
String fileName1 = "example.txt";
String fileName2 = "example2.txt";
String fileName3 = "example3.txt";
String fileName4 = "example4.txt";
String fileName5 = "example5.txt";
printFileSize("tester.txt");
String testWorld = "";
// 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), testWorld.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
hashFile("hellohi.txt");
// if(hashFile("moviescript.txt").equals("f2931088e20bc4d3fb70d06a655cba8b053dd0279b5c112a0812ae5fc2c4e05f")){
// System.out.println("works equal");
// }
}
public static void hiddenFileCreator(String data, String fileName) {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName))) {
bufferedWriter.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void fileInPriv(String data, String fileName, String secretFolder) {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName))) {
bufferedWriter.write(data);
Path oldPath = Paths.get("./" + fileName);
Path newPath = Paths.get(secretFolder + "/" + fileName);
Files.move(oldPath, newPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
// Calculate and print the file size using the File class
private static void printFileSize(String fileName) {
File file = new File("./" + fileName);
System.out.println(file.length());
}
public static String toString(String fileName) {
return "File Name Is: filewriter";
}
public static String hashFile(String filePath) throws IOException {
File file = new File(filePath);
if(!file.exists()){
throw new FileNotFoundException("file doesnt exist!");
}
Path pathToFile = Paths.get("./" + filePath);
byte[] fileBytes = Files.readAllBytes(pathToFile);
String fileString = new String(fileBytes);
System.out.println(fileString);
try {
// Get an instance of the SHA-256 message digest algorithm
MessageDigest md = MessageDigest.getInstance("SHA-256");
// Compute the hash of the input string
byte[] hash = md.digest(fileString.getBytes());
// Convert the byte array hash to a hexadecimal string
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
hexString.append(String.format("%02x", b));
}
System.out.println(hexString.toString());
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
// Handle the case where SHA-256 algorithm is not available
e.printStackTrace();
return null;
}
}
}