-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandlingActivity.java
More file actions
111 lines (94 loc) · 3.83 KB
/
FileHandlingActivity.java
File metadata and controls
111 lines (94 loc) · 3.83 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
import java.io.*;
public class FileHandlingActivity {
public static void createDirectory(String dirPath, String dirName){
if (!dirPath.equals("")){
dirPath += "/";
}
File dir = new File(dirPath + dirName);
dir.mkdir();
}
public static void createFile(String dirPath, String fileName){
File f = new File(dirPath + "/" + fileName);
try {
f.createNewFile();
} catch (IOException e){
e.printStackTrace();
}
}
public static void writeToFile(String filePath, String content){
try (FileWriter writer = new FileWriter(filePath)) {
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String readFile(String filePath){
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
if (sb.length() > 1){
sb.deleteCharAt(sb.length() - 1); //remove terminator
}
return sb.toString();
}
public static File[] listFiles(String dirPath){
File dir = new File(dirPath);
return dir.listFiles();
}
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"); //FIXED: removed '/'
// 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();
}
}
public static void main(String[] args) {
// Your code here
// a. Create main directory
createDirectory("", "JavaFileSystem");
// b. Create three text files
createFile("JavaFileSystem", "notes.txt");
createFile("JavaFileSystem", "log.txt");
createFile("JavaFileSystem", "data.txt");
// c. Write messages to files
writeToFile("JavaFileSystem/notes.txt", "This is the notes file!");
writeToFile("JavaFileSystem/log.txt", "This is the second file, the log file!");
writeToFile("JavaFileSystem/data.txt", "This is the third file, the data file!");
// d. Read and display file contents
System.out.println(readFile("JavaFileSystem/notes.txt"));
System.out.println(readFile("JavaFileSystem/log.txt"));
System.out.println(readFile("JavaFileSystem/data.txt"));
// e. Create backup directory
createDirectory("", "JavaFileSystem/Backup");
// f. Copy contents to backup file
String combinedData = "NOTES:\n" + readFile("JavaFileSystem/notes.txt") +
"\nLOG:\n" + readFile("JavaFileSystem/log.txt") + "\nDATA:\n" + readFile("JavaFileSystem/data.txt");
createFile("JavaFileSystem/Backup", "backup.txt");
writeToFile("JavaFileSystem/Backup/backup.txt", combinedData);
// g. List all files in both directories
File[] JavaFileSystemFiles = listFiles("JavaFileSystem");
File[] BackupFiles = listFiles("JavaFileSystem/Backup");
System.out.println("Main files:");
for (File f : JavaFileSystemFiles){
System.out.println(f.getName());
}
System.out.println("Backup files:");
for (File f : BackupFiles){
System.out.println(f.getName());
}
debugFileOperation();
}
}