Document

指定一个目录下所有的java文件,把里面的内容格式化输出在md文件

# 指定一个目录下所有的java文件,把里面的内容格式化输出在md文件

```java

 


import java.io.*;

/**
* @author Mxhlin
* @Email fuhua277@163.com
* @Date 2022/09/22/17:08
* @Version
* @Description 指定一个目录下所有的java文件,把里面的内容格式化输出在md文件
*/
public class AllCodeDemo {
static int sum= 0 ;
public static void main(String[] args) {
all(new File("D:\\peixun\\java\\Lx"));
}

public static void all(File file){
if (file.isDirectory()){
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isDirectory()) all(file1);
if (file1.isFile()&&file1.getName().endsWith(".java")){
all(++sum,file1,new File(".md"));
}
}
}
if (file.isFile()&&file.getName().endsWith(".java")){
all(++sum,file,new File(".md"));
}
}

public static void all(int n, File src,File dst){
String template = """
%d %s (%d %tF %<tT)
```java
%s
```

""";
try (FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(dst,true)){
// 根据上面的参数 格式化
sum = n;
String path = src.getAbsolutePath();
String s = new String(fis.readAllBytes());
long count = s.lines().count();
long l = src.lastModified();
String format = String.format(template, sum, path, count, l, s);
fos.write(format.getBytes());
fos.flush();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

 

```

posted @ 2022-09-23 08:33  一蓑烟雨任平生。。  阅读(146)  评论(0)    收藏  举报
Document