-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandlingActivity.java
More file actions
105 lines (89 loc) · 3.77 KB
/
FileHandlingActivity.java
File metadata and controls
105 lines (89 loc) · 3.77 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
public class FileHandlingActivity {
public static void main(String[] args) {
// Your code here
// a. Create main directory
File dir = new File("JavaFileSystem");
dir.mkdir();
// b. Create three text files
try {
Files.createFile(Paths.get("JavaFileSystem/notes.txt"));
Files.createFile(Paths.get("JavaFileSystem/data.txt"));
Files.createFile(Paths.get("JavaFileSystem/log.txt"));
} catch (Exception e) {
// TODO: handle exception
}
// c. Write messages to files
try {
Files.write(Paths.get("JavaFileSystem/notes.txt"), "Fortnite".getBytes(StandardCharsets.UTF_8));
Files.write(Paths.get("JavaFileSystem/data.txt"), "Battle".getBytes(StandardCharsets.UTF_8));
Files.write(Paths.get("JavaFileSystem/log.txt"), "Pass".getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
// TODO: handle exception
}
// d. Read and display file contents
try {
String notes = new String(Files.readAllBytes(Paths.get("JavaFileSystem/notes.txt")));
String data = new String(Files.readAllBytes(Paths.get("JavaFileSystem/data.txt")));
String log = new String(Files.readAllBytes(Paths.get("JavaFileSystem/log.txt")));
System.out.println("notes.txt: " + notes);
System.out.println("data.txt: " + data);
System.out.println("log.txt: "+log);
} catch (Exception e) {
// TODO: handle exception
}
// e. Create backup directory
Path dirr = Paths.get(Paths.get("JavaFileSystem").toString(), "Backup");
try {
Files.createDirectories(dirr);
} catch (Exception e) {
// TODO: handle exception
}
// f. Copy contents to backup file
try {
String notes = new String(Files.readAllBytes(Paths.get("JavaFileSystem/notes.txt")));
String data = new String(Files.readAllBytes(Paths.get("JavaFileSystem/data.txt")));
String log = new String(Files.readAllBytes(Paths.get("JavaFileSystem/log.txt")));
String text = notes + data + log;
Files.write(Paths.get("JavaFileSystem/Backup").resolve(Paths.get("backup.txt")), text.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
// TODO: handle exception
}
// g. List all files in both directories
try {
String str = new String();
File main = new File("JavaFileSystem");
File backup = new File("JavaFileSystem/Backup");
str += "Contains:\n";
for (File file : main.listFiles()) {
if (file.toString().contains(".txt")) {
str += file + "\n";
}
}
for (File file : backup.listFiles()) {
str += file + "\n";
}
System.out.println(str.trim());
} catch (Exception e) {
// TODO: handle exception
}
debugFileOperation();
}
public static void debugFileOperation() {
try {
// Creating a file with an valid name
File file = new File("fileName.txt");
// Attempting to write to the valid file
FileWriter writer = new FileWriter(file);
writer.write("Will this fail?");
writer.close();
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
e.printStackTrace();
}
}
}