-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandlingActivity.java
More file actions
87 lines (58 loc) · 2.16 KB
/
FileHandlingActivity.java
File metadata and controls
87 lines (58 loc) · 2.16 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
import java.io.*;
public class FileHandlingActivity {
// creates a directory
public static void makeDirectory(String directoryPath) {
File mainDir = new File(directoryPath);
mainDir.mkdir();
}
// creates text files
public static void makeFile(String filePath) throws IOException {
File file = new File(filePath);
file.createNewFile();
}
public static void writeFile(String filePath, String message) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
writer.write(message);
writer.close();
}
public static String readFile(String filePath) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
String contents = "";
while ((line = reader.readLine()) != null) {
contents += line;
}
reader.close();
return contents;
}
public static void cloneFile(String newFile, String oldFile) throws IOException {
// get to the end of the new file so you don't overwrite anything
BufferedReader newReader = new BufferedReader(new FileReader(newFile));
BufferedReader oldReader = new BufferedReader(new FileReader(oldFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(newFile, true));
// for writing
String line;
// get until the end of the new file's writing
while ((line = newReader.readLine()) != null) {
// keep reading
}
newReader.close();
// write the contents of the file
writer.write("==" + oldFile + "==\n");
while ((line = oldReader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
writer.write("\n");
oldReader.close();
writer.close();
}
public static String listFiles(String directory) {
File[] files = new File(directory).listFiles();
String fileOutput = "";
for (File file : files) {
fileOutput += file + "\n";
}
return fileOutput;
}
}