-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandlingActivity.java
More file actions
160 lines (134 loc) · 5.23 KB
/
FileHandlingActivity.java
File metadata and controls
160 lines (134 loc) · 5.23 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.io.*;
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
File notes = new File("JavaFileSystem/notes.txt");
File data = new File("JavaFileSystem/data.txt");
File log = new File("JavaFileSystem/log.txt");
try {
notes.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
try {
data.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
try {
log.createNewFile();
} catch (Exception e) {
System.out.println(e.getMessage());
}
// c. Write messages to files
try (BufferedWriter bw = new BufferedWriter(new FileWriter("JavaFileSystem/notes.txt"))) {
bw.write("Don't forget to commit at each step!");
} catch (IOException e) {
System.out.println(e.getMessage());
}
try (BufferedWriter bw = new BufferedWriter(new FileWriter("JavaFileSystem/data.txt"))) {
bw.write("01100100 01100001 01110100 01100001");
} catch (IOException e) {
System.out.println(e.getMessage());
}
try (BufferedWriter bw = new BufferedWriter(new FileWriter("JavaFileSystem/log.txt"))) {
bw.write("File made!\nFile edited!");
} catch (IOException e) {
System.out.println(e.getMessage());
}
// d. Read and display file contents
try (BufferedReader br = new BufferedReader(new FileReader("JavaFileSystem/notes.txt"))) {
System.out.println("notes.txt:");
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
System.out.println();
try (BufferedReader br = new BufferedReader(new FileReader("JavaFileSystem/data.txt"))) {
System.out.println("data.txt:");
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
System.out.println();
try (BufferedReader br = new BufferedReader(new FileReader("JavaFileSystem/log.txt"))) {
System.out.println("log.txt:");
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
System.out.println();
// e. Create backup directory
File subDir = new File("JavaFileSystem/Backup");
subDir.mkdir();
// f. Copy contents to backup file
try (BufferedWriter bw = new BufferedWriter(new FileWriter("JavaFileSystem/Backup/backup.txt"))) {
for (String fileName : new String[] { "notes.txt", "data.txt", "log.txt" }) {
try (BufferedReader br = new BufferedReader(new FileReader("JavaFileSystem/" + fileName))) {
bw.write(fileName + ":\n");
String line = br.readLine();
while (line != null) {
bw.write(line + "\n");
line = br.readLine();
}
bw.write("\n");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
// g. List all files in both directories
if (dir.exists() && dir.isDirectory()) {
System.out.println("In " + dir.getName() + ":");
File[] files = dir.listFiles();
for (File file : files) {
if (file.isFile()) {
System.out.println(file.getName());
}
}
}
System.out.println();
if (subDir.exists() && subDir.isDirectory()) {
System.out.println("In JavaFileSystem/" + subDir.getName() + ":");
File[] files = subDir.listFiles();
for (File file : files) {
if (file.isFile()) {
System.out.println(file.getName());
}
}
}
System.out.println();
debugFileOperation();
}
public static void debugFileOperation() {
try {
// Creating a file with an invalid name (forward slash is invalid for file names on many OS)
File file = new File("fileName.txt");
// Attempting to write to the invalid 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();
}
}
}