导出文件夹下的源码到指定文本文件,用于软著时使用

package com.company;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Main {

    public static void main(String[] args) throws IOException {
        List<File> allFile = getAllFile("D:\\coordination");
        ArrayList<String> strings = new ArrayList<>();
        for (File file :allFile) {
            System.out.println(file);
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            String line;
            //网友推荐更加简洁的写法
            while ((line = bufferedReader.readLine()) != null) {
                // 一次读入一行数据
                if (!line.equals("")){
                    strings.add(line);
                }
            }
        }
        File writeName = new File("D:\\source.txt");
        writeName.createNewFile();
        FileWriter writer = new FileWriter(writeName);
        BufferedWriter out = new BufferedWriter(writer);
        for (String string : strings) {
            out.write(string+"\r\n");
            out.flush();
        }
    }

    public static List<File> getAllFile(String dirFilePath) {
        return getAllFile(new File(dirFilePath));
    }
    
    public static  List<File> getAllFile(File dirFile) {
        if (Objects.isNull(dirFile) || !dirFile.exists() || dirFile.isFile())
            return null;

        File[] childrenFiles = dirFile.listFiles();
        if (Objects.isNull(childrenFiles) || childrenFiles.length == 0)
            return null;

        List<File> files = new ArrayList<>();
        for (File childFile : childrenFiles) {
            if (childFile.isFile()) {
                files.add(childFile);
            }else {
                List<File> cFiles = getAllFile(childFile);
                if (Objects.isNull(cFiles) || cFiles.isEmpty()) continue;
                files.addAll(cFiles);
            }
        }
        return files;
    }

}

  

posted @ 2023-02-15 14:31  梦-咖啡  阅读(136)  评论(0编辑  收藏  举报