-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileIO.java
More file actions
132 lines (116 loc) · 4.8 KB
/
FileIO.java
File metadata and controls
132 lines (116 loc) · 4.8 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
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FileIO {
// Makes a directory within the current working directory (usually gat/).
// Returns true if succeeded in making the directory and false if it failed (i.e. the directory already existed).
public static boolean makeDirectory(String directoryName) {
File dir = new File(directoryName);
if (dir.exists()) {
return false;
}
return dir.mkdir();
}
// Makes the specified file in the current working directory (usually gat/).
// Returns true if succeeded
public static boolean makeFile(String fileName) {
File file = new File(fileName);
if (file.exists()) {
return false;
}
try {
return file.createNewFile();
} catch (IOException e) {
System.out.println(e.getMessage());
return false;
}
}
// Writes the specified content to the file referenced by fileName
// NOTE: any data previously in the file will be deleted!
// If there is no such file with name fileName, the file is first created
// Returns true if succeeded in writing the data, false otherwise
public static boolean writeToFile(String fileName, String content) {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName))) {
bufferedWriter.write(content);
bufferedWriter.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
// Appends the specified content to the file referenced by fileName
// NOTE: any data previously in the file will NOT be deleted!
// If there is no such file with name fileName, the file is first created
// Returns true if succeeded in writing the data, false otherwise
public static boolean appendToFile(String fileName, String content) {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName, true))) {
bufferedWriter.write(content);
bufferedWriter.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
// Reads the specified file
// Returns the file's contents, or null if reading failed
public static String readFile(String fileName) {
try {
return new String(Files.readAllBytes(Paths.get(fileName)), StandardCharsets.UTF_8);
} catch (Exception e) {
System.out.println(e.getMessage());
return null;
}
}
// builds the working list from the index file and writes it to git/working
public static List<String> buildWorkingList(File indexFile) throws IOException {
List<String> lines = Files.readAllLines(indexFile.toPath());
List<String> workingList = new ArrayList<>();
for (String line : lines) {
if (line.trim().isEmpty()) continue;
String[] parts = line.split(" ");
if (parts.length < 2) continue;
String fileName = parts[0];
String sha = parts[1];
workingList.add("blob " + sha + " " + fileName);
}
Files.write(Paths.get("git/working"), workingList);
return workingList;
}
// groups working list entries by their parent directories
public static Map<String, List<String>> groupByParentDirectory(List<String> workingList) {
Map<String, List<String>> dirMap = new HashMap<>();
for (String entry : workingList) {
String[] parts = entry.split(" ");
if (parts.length < 3) continue;
String fullPath = parts[2];
File f = new File(fullPath);
String parent = (f.getParent() == null) ? "" : f.getParent();
dirMap.computeIfAbsent(parent, k -> new ArrayList<>()).add(entry);
}
return dirMap;
}
// sorts directories from deepest to shallowest for recursive tree creation
public static List<String> sortDirectoriesByDepth(Map<String, List<String>> dirMap) {
List<String> dirs = new ArrayList<>(dirMap.keySet());
dirs.sort((a, b) -> Integer.compare(b.split("/").length, a.split("/").length));
return dirs;
}
// writes a tree file to git/objects and returns its SHA1 hash
public static String writeTreeObject(String content) throws IOException {
String sha = gat.SHA1(content);
Path path = Paths.get("git/objects", sha);
Files.write(path, content.getBytes());
return sha;
}
}