批量给java文件添加license声明

2019-07-23  本文已影响0人  Coder蒋

JLicenseForJavaFileUtil说明文档

主要功能介绍

关键代码介绍

private static void iterativeHandleFiles(File file, String fileSuffix, FileAction... action) {

        if (file == null || !file.exists() || action == null) {
            return;
        }

        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files == null) {
                return;
            }
            for (int i = 0; i < files.length; i++) {
                iterativeHandleFiles(files[i], fileSuffix, action);
            }
        } else {
            String name = file.getName();
            // 不是目标文件则直接返回,不处理
            if (isEmpty(name) || !name.endsWith(FILE_SUFFIX)) {
                return;
            }
            for (int i = 0; i < action.length; i++) {
                action[i].handleFile(file);
            }
        }
    }
 public void handleFile(File file) {

        RandomAccessFile targetRandomAccessFile = null;
        try {
            targetRandomAccessFile = new RandomAccessFile(file, "rw");

            if (targetRandomAccessFile.length() > MAX_FILE_SIZE) {
                System.out.println("file size is too long!" + file.getName());
                return;
            }

            // 读取license文本内容
            byte[] contentBytes = new byte[(int) targetRandomAccessFile.length()];
            targetRandomAccessFile.readFully(contentBytes);
            String contentStr = new String(contentBytes);

            int indexOfPackage = contentStr.indexOf("package");
            // 拼接最终的文件内容
            contentStr = mLicenseStr + "\n" + contentStr.substring(indexOfPackage);
            targetRandomAccessFile.seek(0);
            targetRandomAccessFile.setLength(contentStr.length());
            targetRandomAccessFile.write(contentStr.getBytes("UTF-8"));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (targetRandomAccessFile != null) {
                    targetRandomAccessFile.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
public void handleFile(File file) {

        RandomAccessFile targetRandomAccessFile = null;
        RandomAccessFile licenseRandomAccessFile = null;
        try {
            targetRandomAccessFile = new RandomAccessFile(file, "rw");
            licenseRandomAccessFile = new RandomAccessFile(mLicensePath, "rw");

            if (targetRandomAccessFile.length() > MAX_FILE_SIZE) {
                System.out.println("file size is too long!" + file.getName());
                return;
            }

            // 读取文本内容
            byte[] contentBytes = new byte[(int) targetRandomAccessFile.length()];
            targetRandomAccessFile.readFully(contentBytes);
            String contentStr = new String(contentBytes);

            // 读取license文本内容
            byte[] licenseBytes = new byte[(int) licenseRandomAccessFile.length()];
            licenseRandomAccessFile.readFully(licenseBytes);
            String licenseStr = new String(licenseBytes);

            int indexOfPackage = contentStr.indexOf("package");
            // 拼接最终的文件内容
            contentStr = licenseStr + "\n" + contentStr.substring(indexOfPackage);
            targetRandomAccessFile.seek(0);
            targetRandomAccessFile.setLength(contentStr.length());
            targetRandomAccessFile.write(contentStr.getBytes("UTF-8"));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (targetRandomAccessFile != null) {
                    targetRandomAccessFile.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (licenseRandomAccessFile != null) {
                try {
                    licenseRandomAccessFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

使用说明

该工程是一个android studio工程,实际上也可以将源码文件拷贝到任何一个java工程中运行。

使用步骤:

反馈渠道

项目地址

github

上一篇 下一篇

猜你喜欢

热点阅读