申请软著Java代码提取

公司要求申请代码软著,需要提取代码

申请软著代码要求:
要满3000行,前1500行和尾1500行,大约word文档 65页。
删除注释和空行

分别找了两个shell脚本和一段Java代码。
shell脚本不太会用,但也贴下面了,给会用的人用。

第一个:

#!/bin/bash 
//定义合并文件方法
list_alldir(){
// 1.递归文件目录
for file2 in `ls -A $1`
do
if [ -d "$1/$file2" ];then
#echo "$1/$file2"
list_alldir "$1/$file2"
elif [ -f  "$1/$file2" ];then
    #2.如果后缀是.java,合并文件
    if [[ "$1/$file2" == *.java ]] ;then
    #echo "\n" >> out.txt
    #echo "$1/$file2" >> out.txt
    #echo "\n" >> out.txt
    cat "$1/$file2" >> out.txt
    fi
fi
    done
}

#src为指定的文件夹
list_alldir ./src

第二个:

#!/bin/bash

set -e	# or use "set -o errexit" to quit on error.
set -x  # or use "set -o xtrace" to print the statement before you execute it.

IFS=$'\n'; set -f
for filepath in $(find ./ -name '*.xml' -or -name '*.kt'); do
	echo $(basename "$filepath")>>output.txt;
	echo "">>output.txt;
	cat $filepath>>output.txt;
	echo "">>output.txt;
	echo "">>output.txt;
done
unset IFS; set +f

添加执行权限,并执行。

chmod +x gen_code.sh
./gen_code.sh

最后用了下面的Java代码,开了一个main方法本地跑的

import java.io.*;

public class SourceExport {

    public static void main(String[] args) throws Exception {

        //文件读取路径
        File dir = new File("D:\\asdfsfd");

        //文件输出路径
        File target = new File("D:\\dst.txt");

        BufferedWriter bw = null;
        bw = new BufferedWriter(new FileWriter(target));
        StringBuffer sb = new StringBuffer();
        loopRead(dir, sb);
        write(sb.toString(), bw);

    }

    /**
     * 遍历文件夹下所有文件
     *
     * @param dir
     * @param sb
     */
    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));
                    }

                }
            }
        }
    }

    /**
     * 读取文件里面的内容
     *
     * @param file
     * @return
     */
    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();
    }

    /**
     * 将读取的路径以及相应的内容写入指定的文件
     *
     * @param str
     * @param writer
     */
    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();
            }
        }
    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值