1.
import java.io.File;
import java.util.HashMap;
public class Main {
public static void listDir(File file, HashMap<String, String> namePathMap) {
if (file.isDirectory()) {
// 是目录则递归调用
File[] results = file.listFiles();
for (int i = 0; i < results.length; i++) {
listDir(results[i], namePathMap);
}
} else {
if (file != null && file.isFile()) {
String name = file.getName();
if (name.endsWith(".js") || name.endsWith(".ts")) {
String path = file.getAbsolutePath();
namePathMap.put(name, path);
}
}
}
}
public static void main(String[] args) {
// 得到Framework下面的文件
HashMap<String, String> frameworkNamePathMap = new HashMap<>();
File file = new File("/Users/jianan/Documents/old_work/sayabc-gamification-demo01-all/MathClassroomH5/Project/IFTTTSample/assets/Framework");
listDir(file, frameworkNamePathMap);
// mario上的文件
HashMap<String, String> marioNamePathMap = new HashMap<>();
File marioFile = new File("/Users/jianan/Documents/new_work/mario/courseware/assets");
listDir(marioFile, marioNamePathMap);
// TODO 第1步: 保证Framework下面不多删
// Framework中的脚本,mario上都可以找到. 这样保证不会多删除。
System.err.println("下面的文件不可以放在Framework目录下");
for (String key : frameworkNamePathMap.keySet()) {
if (marioNamePathMap.get(key) == null) {
System.out.println(key + ":" + frameworkNamePathMap.get(key));
}
}
// TODO 第2步: 不少删
// mario上有的,旧工程也有,那么必然是在Framework中。
HashMap<String, String> oldNamePathMap = new HashMap<>();
File oldFile = new File("/Users/jianan/Documents/old_work/sayabc-gamification-demo01-all/MathClassroomH5/Project/IFTTTSample/assets");
listDir(oldFile, oldNamePathMap);
System.err.println("应该放到Framework目录下的文件");
for (String key : marioNamePathMap.keySet()) {
if (oldNamePathMap.get(key) != null) {
String path = oldNamePathMap.get(key);
int idx = path.indexOf("MathClassroomH5/Project/IFTTTSample/assets/Framework/");
if (idx == -1) {
System.out.println(key + ":" + path);
}
}
}
}
}
/*
下面的文件不可以放在Framework目录下
应该放到Framework目录下的文件
*/
2.
package org.example.testFile2;
import java.io.File;
public class Main {
public static void main(String[] args) throws Exception {
String path = "D:/csv/csv";
String fileName = "test";
String filePath = path + "/" + fileName + ".csv";
File file = new File(filePath);
// 父文件夹不存在则创建
File parentFile = file.getParentFile();
if (parentFile != null) {
parentFile.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
}
}