# 指定一个目录下所有的java文件,把里面的内容格式化输出在md文件
```java
import java.io.*;
/**
* @author Mxhlin
* @Email [email protected]
* @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);
}
}
}
```
标签:文件,file1,java,String,md,file,File,new From: https://www.cnblogs.com/xhlin/p/16721468.html