import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
public class FileSystem {
public static final String path = "D:\\OneDrive\\java\\notebook - 副本";
public static final String[] excludeDir = new String[]{".git","notebook_asset"};
public static void main(String[] args) throws IOException {
File f = new File(path);
ArrayList<MDClass> mdClasses = new ArrayList<>();
ArrayList<String> mdSidebar = new ArrayList<>();
tree(f, 1, mdClasses,mdSidebar);
ArrayList<MDClass> list = tree2(mdClasses, "");
genderJson(list);
genderSidebar(mdSidebar);
}
private static void genderSidebar(ArrayList<String> mdSidebar) throws IOException {
String str = "";
for (String s : mdSidebar) {
str += s +"\r\n";
}
copyContent( path + "\\_sidebar.md", path + "\\_sidebar_old_" + String.valueOf(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())) + ".md");
File file = new File(path + "\\_sidebar.md");
FileOutputStream fos = new FileOutputStream(file);
fos.write(str.getBytes());
fos.flush();
fos.close();
}
private static void genderJson(ArrayList<MDClass> list) throws IOException {
ObjectMapper om=new ObjectMapper();
String json = om.writeValueAsString(list);
System.out.println(json);
copyContent( path + "\\content.json", path + "\\content_old_" + String.valueOf(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())) + ".json");
File file = new File(path + "\\content.json");
FileOutputStream fos = new FileOutputStream(file);
fos.write(json.getBytes());
fos.flush();
fos.close();
}
private static void copyContent( String fromPath, String goPath) throws IOException {
File fromFile = new File(fromPath);
if (!fromFile.exists()) {
fromFile.createNewFile();
}
File goFile = new File(goPath);
if (!goFile.exists()) {
goFile.createNewFile();
}
InputStream in = new FileInputStream(fromFile);
OutputStream out = new FileOutputStream(goFile);
byte[] buf = new byte[2048];
int i = 0;
while ((i = in.read(buf)) != -1) {
out.write(buf, 0, i);
out.flush();
}
out.close();
in.close();
System.out.println("文件复制结束!!!");
}
private static ArrayList<MDClass> tree2(ArrayList<MDClass> list, String parentPath) {
ArrayList<MDClass> newlist = new ArrayList<>();
Iterator<MDClass> iterator = list.iterator();
while (iterator.hasNext()) {
MDClass mdClass = iterator.next();
if (mdClass.getParentUrl().equals(parentPath)) {
newlist.add(mdClass);
tree2(list, mdClass.getUrl());
}
}
for (MDClass mdClass : newlist) {
ArrayList<MDClass> children = new ArrayList<>();
for (MDClass obj : list) {
if (obj.getParentUrl().equals(mdClass.getUrl())) {
children.add(obj);
}
}
mdClass.setChildren(children);
}
return newlist;
}
private static void tree(File f, int level, ArrayList<MDClass> list, ArrayList<String> mdSidebar) {
ArrayList<MDClass> mdClasses = new ArrayList<>();
File[] childs = f.listFiles();
for (int i = 0; i < childs.length; i++) {
String space = "";
for (int i1 = 0; i1 < level-1; i1++) {
space += " ";
}
File child = childs[i];
String name = child.getName();
String url = child.getAbsolutePath().replace(path, "").replace("\\", "/");
if (child.isDirectory()) {
if (!Arrays.asList(excludeDir).contains(child.getName())) {
mdSidebar.add(space + "* "+child.getName());
list.add(new MDClass(Math.random() + "", child.getName(), url, "2", mdClasses, child.getParent().replace(path, "").replace("\\", "/"), child.getName()));
tree(child, level + 1, list, mdSidebar);
}
} else {
if (name.endsWith(".md") && !name.startsWith("_sidebar") && !name.startsWith("_navbar")) {
String label = child.getName().replace(".md", "");
String mdUrl = "* [" + label + "](" + url + ")";
mdSidebar.add(space + mdUrl);
list.add(new MDClass(Math.random() + "", label, url, "1", mdClasses, child.getParent().replace(path, "").replace("\\", "/"), mdUrl));
}
}
}
}
}
- 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
- 161
![在这里插入图片描述](https://img-blog.csdnimg.cn/884dcdb4e7524800ba720497d638245c.png)