When the company made me write softly ...

The company has to write softly, let me copy the project code into word.
I started dumbly copying a few java classes, and then suddenly realized that things that can be solved with a program will never be used, because I will often use them in the future. Backup, although it is very simple, a recursive + read and write files .

To make it a bit more complicated, consider file encoding and automatic filtering. If you only count the number of lines of code, then the IDEA Statisticplug-in must be very suitable.

    
    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;
            }
        }
    }

Published 80 original articles · Like 319 · Visits 340,000+

Guess you like

Origin blog.csdn.net/jimo_lonely/article/details/105056795