-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyFileWriter.java
More file actions
73 lines (64 loc) · 2.47 KB
/
MyFileWriter.java
File metadata and controls
73 lines (64 loc) · 2.47 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
64
65
66
67
68
69
70
71
72
73
import java.io.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
public class MyFileWriter {
public static void main(String[] args) {
String secretName = "Barack";
String secretCode = "8241763509";
// 1. Using FileWriter
// try (FileWriter writer = new FileWriter(fileName1)) {
// writer.write(data);
// } catch (IOException e) {
// e.printStackTrace();
// }
// 2. Using BufferedWriter
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(".Obama's Last Name.txt"))) {
bufferedWriter.write(secretName);
} catch (IOException e) {
e.printStackTrace();
}
// 3. Using FileOutputStream
// try (FileOutputStream outputStream = new FileOutputStream(fileName3)) {
// outputStream.write(data.getBytes());
// } catch (IOException e) {
// e.printStackTrace();
// }
// 4. Using BufferedOutputStream
try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(".secretInfo/Theiss Bank Account Number.dat"))) {
bufferedOutputStream.write(secretCode.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
// 5. Using Files (java.nio.file)
// try {
// Files.write(Paths.get(fileName5), data.getBytes(StandardCharsets.UTF_8));
// } catch (IOException e) {
// e.printStackTrace();
// }
}
// 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());
}
private static long fileSize(String fileName) {
File file = new File(fileName);
return file.length();
}
private static void printTotalFileSize(String... fileNames) {
long length = 0;
for (String file : fileNames)
length += fileSize(file);
System.out.println("Total size of all files: " + length + " bytes");
}
private static void printTotalFileSize2(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");
}
}