forked from nlevin11/directory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyFileWriter.java
More file actions
45 lines (36 loc) · 1.45 KB
/
MyFileWriter.java
File metadata and controls
45 lines (36 loc) · 1.45 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
import java.io.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
public class MyFileWriter {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(".hiddenpassword.txt"));
bw.write ("lake123");
bw.close();
printFileSize(".hiddenpassword.txt");
File hiddenFolder = new File (".hiddenFolder");
if (!hiddenFolder.exists())
hiddenFolder.mkdir();
File hiddenFile = new File (hiddenFolder, ".classified.dat");
BufferedWriter bw2 = new BufferedWriter(new FileWriter(hiddenFile));
bw2.write ("this is top secret");
bw2.close();
printFileSize (".hiddenFolder/.classified.dat");
printTotalFileSize(".hiddenpassword.txt", ".hiddenFolder/.classified.dat");
}
// Calculate and print the file size using the File class
private static void printFileSize(String fileName) {
File file = new File (fileName);
long bytes = file.length();
System.out.println (bytes);
}
private static void printTotalFileSize(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");
}
}