forked from angelayuan1/FileWriterActivity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyFileWriter.java
More file actions
60 lines (49 loc) · 1.78 KB
/
MyFileWriter.java
File metadata and controls
60 lines (49 loc) · 1.78 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
import java.io.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
public class MyFileWriter {
public static void method1() {
String name = ".secret.txt";
String password = "password";
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(name))) {
bufferedWriter.write(password);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void method2() {
String folderName = ".topsecret";
String fileName = "confidentialfile.dat";
String confidentialInfo = "super top secret confidential info";
File hiddenFolder = new File(folderName);
hiddenFolder.mkdir();
File confidentialFile = new File(hiddenFolder, fileName);
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(confidentialFile))) {
bufferedWriter.write(confidentialInfo);
} 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");
}
public String toString() {
return "Hello World";
}
public static void main(String[] args) {
// test each file individually
printFileSize("testFile.txt"); // 13
printFileSize("testFile2.txt"); // 39
printFileSize("testFile3.txt"); // 15
// all together
printFileSize("testFile.txt", "testFile2.txt", "testFile3.txt"); // 67
}
}