軟著整理程式碼快速生成

qq_43525481發表於2020-11-21
package com.demo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Writer;

/**
 * @author lisa
 * @date 2020/11/20 - 20:42
 */


    /**
     * 申請軟著程式碼複製刪除註釋和空行
     */
    public class SourceExport {
        public static void main(String[] args) throws Exception {
            //檔案讀取路徑
//            File dir = new File("E:\\competition\\miniprogram-34\\miniprogram-34\\pages\\index");
//       1     File dir = new File("E:\\competition\\miniprogram-34\\miniprogram-34\\pages\\main");
//            File dir = new File("E:\\competition\\miniprogram-34\\miniprogram-34\\pages\\collection");
//         2   File dir = new File("E:\\competition\\miniprogram-34\\miniprogram-34\\pages\\creativity");
//            File dir = new File("E:\\competition\\miniprogram-34\\miniprogram-34\\pages\\main_04");
//          3  File dir = new File("E:\\competition\\miniprogram-34\\miniprogram-34\\pages\\main_04_pages_01");
//          4  File dir = new File("E:\\competition\\miniprogram-34\\miniprogram-34\\pages\\creativity\\chuangyishangpingdetail\\chuangyidetail01");
//        5    File dir = new File("E:\\competition\\miniprogram-34\\miniprogram-34\\pages\\main_02");
//          6  File dir = new File("E:\\competition\\miniprogram-34\\miniprogram-34\\pages\\main_08");
//          7  File dir = new File("E:\\competition\\miniprogram-34\\miniprogram-34\\pages\\myadd");
//         8   File dir = new File("E:\\competition\\miniprogram-34\\miniprogram-34\\pages\\feedback");
            File dir = new File("E:\\competition\\miniprogram-34\\miniprogram-34\\pages\\feedback");
//            File dir = new File("C:\\Users\\Administrator\\Desktop\\aaa\\src");
            //檔案輸出路徑
//            File target = new File("C:\\Users\\Administrator\\Desktop\\aaa\\dst\\dst.txt");
            File target = new File("E:\\code\\feedback.txt");
            BufferedWriter bw = new BufferedWriter(new FileWriter(target));

            StringBuffer sb = new StringBuffer();
            loopRead(dir, sb);
            write(sb.toString(), bw);
        }

        // 遍歷資料夾下所有檔案
        private static void loopRead(File dir, StringBuffer sb) {
            File[] files = dir.listFiles();
            if (files != null)
                for (File file : files) {
                    if (file.isDirectory()) {
                        loopRead(file, sb);
                    } else {
                        if (file.length() != 0) {
                            sb.append(readFileToString(file));
                        }
                    }

                }

        }

        //讀取檔案裡面的內容
        private static String readFileToString(File file) {
            BufferedReader br = null;
            StringBuilder sb = new StringBuilder();
            try {
                br = new BufferedReader(new FileReader(file));
                String line = null;
                while ((line = br.readLine()) != null) {
                    String s = line.trim();
                    if (s.length() == 0) {
                        continue;
                    }
                    if (s.startsWith("/") || s.startsWith("*")) {
                        continue;
                    }
                    sb.append(line).append("\n");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null) {
                        br.close();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            return sb.toString();

        }

        //將讀取的路徑以及相應的內容寫入指定的檔案
        private static void write(String str, Writer writer) {
            try {
                writer.write(str);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {

                try {
                    if (writer != null)
                        writer.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }

        }
    }

相關文章