-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyFileWriter.java
More file actions
38 lines (34 loc) · 1.25 KB
/
MyFileWriter.java
File metadata and controls
38 lines (34 loc) · 1.25 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
import java.io.*;
public class MyFileWriter{
public static void main (String [] args)
{
new File(".secretFolder").mkdirs();
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(".secretFolder/thePassword.dat"))) {
bufferedWriter.write("kevinsFeet");
} catch (IOException e) {
e.printStackTrace();
}
printFileSize(".secretFolder/thePassword.dat");
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(".mariahsInfo.txt"))) {
bufferedWriter.write("banana123");
} catch (IOException e) {
e.printStackTrace();
}
printFileSize(".mariahsInfo.txt");
printTotalFileSize(".mariahsInfo.txt", ".gitignore", ".secretFolder");
}
public static void printFileSize(String file)
{
System.out.println(file.length());
}
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");
}
}