當公司讓我寫軟著...

公司要寫軟著,讓我把項目代碼複製到word裏。
我開始還傻傻的複製了幾個java類,然後突然醒悟:能用程序解決的事,絕不動手,因爲以後還會經常用到,備份下,雖然及其簡單,一個遞歸+讀寫文件。

再複雜一點,考慮文件編碼和自動過濾。如果只是單純統計代碼行數,那IDEA的Statistic插件一定很適合。

    
    import org.junit.jupiter.api.Test;
    
    import java.io.*;
    
    /**
     * 統計代碼行數
     *
     * @author jimo
     * @version 1.0.0
     * @date 2020/3/20 19:07
     */
    public class CountCodeLineTest {
    
        @Test
        void copyAllCodeToFile() throws IOException {
    
            String src = "D:\\workspace\\app\\src\\main";
            String output = "D:\\export\\code.txt";
    
            final CountCode test = new CountCode(src, output);
            System.out.println(test.getTotal());
        }
    
        class CountCode {
    
            /**
             * 源碼路徑
             */
            private String srcPath;
            /**
             * 輸出路徑
             */
            private String outPath;
            /**
             * 總行數
             */
            private int total;
            /**
             * 計算哪些文件後綴
             */
            private String[] suffix = {"java", "conf", "xml", "sql"};
    
            public CountCode(String srcPath, String outPath) throws IOException {
                this.srcPath = srcPath;
                this.outPath = outPath;
                countCode();
            }
    
            private void countCode() throws IOException {
                final BufferedWriter bw = new BufferedWriter(new FileWriter(outPath));
                recursiveRead(new File(srcPath), bw);
                bw.close();
            }
    
            private void recursiveRead(File file, BufferedWriter bw) throws IOException {
                // 遞歸讀取文件夾
                if (file.isDirectory()) {
                    final File[] files = file.listFiles();
                    if (files == null || files.length == 0) {
                        return;
                    }
                    for (File f : files) {
                        recursiveRead(f, bw);
                    }
                } else {
                    if (shouldCount(file.getName())) {
                        readFileLine(file, bw);
                    }
                }
            }
    
    
            private boolean shouldCount(String name) {
                for (String s : suffix) {
                    if (name.endsWith(s)) {
                        return true;
                    }
                }
                return false;
            }
    
            private void readFileLine(File file, BufferedWriter bw) throws IOException {
                final BufferedReader br = new BufferedReader(new FileReader(file));
                String line;
                while ((line = br.readLine()) != null) {
                    bw.write(line);
                    bw.write("\n");
                    total++;
                }
                br.close();
            }
    
            public int getTotal() {
                return total;
            }
        }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章