导出项目下全部Java代码

package cn.topspeeder.grow.util;

/**
 * Created by zhaoyx on 2018-11-27 14:32
 */
import java.io.*;

public class ExportCode {

    static FileWriter fw;
    static BufferedWriter writer;
    static String headTitle = "=======================================";
    static String endline="\n";
    public ExportCode() {}
    public ExportCode(String outputPath)
    {
        String os=System.getProperties().getProperty("os.name");
        if(os.startsWith("win")||os.startsWith("Win"))endline="\r\n";
        try {
            // 设置成尾部追加方式
            fw = new FileWriter(outputPath, true);
            writer = new BufferedWriter(fw);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * @param path  绝对路径
     * @param filename 要读的文件名
     */
    public void WriteToMyFile(String path, String filename) {
        if (!filename.endsWith(".java") )
            return;
        try {
            writer.write(endline+headTitle+endline);
            writer.write("『"+filename+"』");
            writer.write(endline+headTitle+endline);
            BufferedReader br = new BufferedReader(new FileReader(path));
            String buf = br.readLine();
            while (buf != null) {
                writer.write(buf + endline);
                buf = br.readLine();
            }
            // 输出到文件
            writer.flush();
            if (br != null)br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    //递归遍历当前文件夹下的所有文件
    public void showAllSubFile(String path) {
        File f = new File(path);
        String[] list = f.list();

        for (String s : list) {
            // System.out.println(s);
            File subf = new File(f.getPath() + File.separator + s);
            // 如果当前s所代表的是文件夹
            if (subf.isDirectory())
                showAllSubFile(subf.getPath());
            else {
                WriteToMyFile(subf.getPath(), s);
            }
        }
    }

    public static void main(String[] args) {

        System.out.println("Export start....");
        /*输入文件夹路径*/
        String inpath="C:\\iWorkSpaces\\center";
        /*输出文件的路径*/
        String outpathString="E:\\DESKTOP\\output.java";
        new ExportCode(outpathString).showAllSubFile(inpath);
        System.out.println("Export Complete.");
        // 最后关掉输出流
        try {
            if (writer != null)
                writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值