forked from hguh-cheng/Activity1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyFileWriter.java
More file actions
38 lines (32 loc) · 1.32 KB
/
MyFileWriter.java
File metadata and controls
38 lines (32 loc) · 1.32 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) {
String password = "helloworld1234";
String data = "glorb";
String hiddenFileName = ".supersecretexample.txt";
String hiddenDirectoryName = ".confidential";
String extraHiddenFileName = ".classified.dat";
// BufferedOutputStream
try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
new FileOutputStream(hiddenFileName))) {
bufferedOutputStream.write(password.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
File hiddenDirectory = new File(hiddenDirectoryName);
if (!hiddenDirectory.exists())
hiddenDirectory.mkdir();
try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
new FileOutputStream(new File(hiddenDirectory, extraHiddenFileName)))) {
bufferedOutputStream.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
printFileSize(hiddenFileName);
}
// 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());
}
}