最近找了一份java 的资料,含有笔记,也很全,但是笔记是一天一天的,想要整体的回顾一下课程,所以需要将不同文件夹的笔记抽取出来,将其合并
/**
* 实现抽取多个小文件的类
*/
class Main {
public static void main(String[] args) throws IOException {
String sourceDir = "F:\\黑马19期\\01、第一阶段java基础";
String outfilePath;
String name = null;
for (int i = 1; i < 28; i++) {
if (i < 10)
name = "0" + i;
else
name = Integer.toString(i);
outfilePath = sourceDir + "\\day" + name + "\\day" + name + "\\"+"笔记+作业" + "\\day" + name + "笔记.md";
//File endFile = new File(outfilePath, sourceDir);
System.out.println(outfilePath);
try {
File startFile = new File(outfilePath);
File tmpFile = new File(sourceDir);//获取文件夹路径
if(!tmpFile.exists()){//判断文件夹是否创建,没有创建则创建新文件夹
tmpFile.mkdirs();
}
System.out.println(sourceDir + startFile.getName());
if (startFile.renameTo(new File(sourceDir + startFile.getName()))) {
System.out.println("File is moved successful!");
} else {
System.out.println("File is failed to move!");
}
} catch (Exception e) {
}
}
}
}
/**
* 实现多个小文件合并的类
*/
class MergeMdFile {
public static void main(String[] args) throws IOException {
//1.给定源路径文件夹
String sourceDir = "F:\\黑马19期\\笔记";
String outfilePath = sourceDir + "\\01、第一阶段java基础day01笔记总结.md";
//2.获取该文件夹下的所有文件(不递归)
List<File> fileList = getFileList(new File(sourceDir));
//3.按照文件名称进行升序
fileList.sort(new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
return o1.getName().compareTo(o2.getName());
}
});
//4.遍历每个小文件并写入目标文件
BufferedWriter bw = new BufferedWriter(new FileWriter(outfilePath, true));
for (File file : fileList) {
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
}
bw.close();
}
//实现文件夹中的文件遍历
public static List<File> getFileList(File file) throws IOException {
List<File> list = new ArrayList<File>();//存放所有的文件对象的结果集合
File[] files = file.listFiles();
for (File file1 : files) {
String file1Name = file1.getName();
//定义一个正则
Pattern p = Pattern.compile("^\\d{2}.+\\.md$");
Matcher m = p.matcher(file1Name);
//如果子文件是文件并且名称符合正则 01....md这个结构(以两位数字开头,以.md结尾),就加入队列
if (file1.isFile() && m.find()) {
list.add(file1);
// System.out.println(file1Name);
}
}
return list;
}
}