-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileWriteExample.java
More file actions
65 lines (53 loc) · 2.03 KB
/
FileWriteExample.java
File metadata and controls
65 lines (53 loc) · 2.03 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
import java.io.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
public class FileWriteExample {
public static void main(String[] args) {
String data = "Hello, World!";
String fileName1 = "example.txt";
String fileName2 = "example2.txt";
String fileName3 = "example3.txt";
String fileName4 = "example4.txt";
String fileName5 = "example5.txt";
// 1. Using FileWriter
File hiddenFile = new File (".topSecret.txt");
try (FileWriter writer = new FileWriter(hiddenFile)) {
writer.write("collinPassword");
} catch (IOException e) {
e.printStackTrace();
}
File hiddenFolder = new File (".superTopSecret");
{
//creates new hidden folder
hiddenFolder.mkdir();
}
File regularFile = new File ("plainTextFile.txt");
{
FileWriter writer2;
try {
writer2 = new FileWriter (regularFile);
writer2.write ("top secret data");
writer2.close();
} catch (IOException e) {
e.printStackTrace();
} //surrounded this with a try catch to handle ioexception
}
//printTotalFileSize("fileName1.txt", ".superTopSecret/fileName2.txt", ".superTopSecret/fileName3.txt");
// ^ this code (if run inside the main) will test the printTotalFileSize method
}
//you don't have the print file size method so i added that too
private static void printFileSize (String fileName) {
File file = new File(fileName);
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");
}
}