JAVA代码添加License

在开源代码的时候,我们经常会在代码顶部添加License信息,每个文件复制粘贴显然是比较麻烦的,所以写了一个工具类方便为源码添加License部分,供大家参考,可以按照实际情况进行修改。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import org.junit.Test;

/**
 * License复制工具
 * 
 * @author jianggujin
 *
 */
public class LicenseCopyUtils implements FileFilter {
   /**
    * 读取License文件
    * 
    * @param in
    * @param charset
    * @return
    * @throws IOException
    */
   public String readLicenseHeader(InputStream in, String charset) throws IOException {
      BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
      StringBuilder builder = new StringBuilder("/**\r\n");
      String line = null;
      while ((line = reader.readLine()) != null) {
         builder.append(" * ");
         builder.append(line);
         builder.append("\r\n");
      }
      builder.append(" */");
      return builder.toString();
   }

   /**
    * 处理license头部信息
    * 
    * @param root
    * @param in
    * @param charset
    * @throws IOException
    */
   public void processLicenseHeader(File root, InputStream in, String charset) throws IOException {
      System.out.println("开始读取并格式化license...");
      String headerBody = readLicenseHeader(in, charset);
      System.out.println(headerBody);
      System.out.println("读取并格式化license完成...");
      if (root.isDirectory() || root.getName().endsWith(".java")) {
         System.out.println("开始处理:" + root.getAbsolutePath());
         processLicenseHeader(root, charset, headerBody);
      }
   }

   private void processLicenseHeader(File root, String charset, String headerBody) throws IOException {
      if (root.isFile()) {
         System.out.println("开始读取并处理:" + root.getAbsolutePath());
         BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(root), charset));
         ByteArrayOutputStream stream = new ByteArrayOutputStream();
         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stream, charset));
         writer.write(headerBody);
         writer.write("\r\n");
         String line = null;
         boolean body = false;
         while ((line = reader.readLine()) != null) {
            if (body || line.startsWith("package ") || line.startsWith("import ")) {
               body = true;
               writer.write(line);
               writer.write("\r\n");
            }
         }
         reader.close();
         writer.close();
         FileOutputStream out = new FileOutputStream(root);
         stream.writeTo(out);
         out.flush();
         out.close();
         System.out.println("读取并处理[" + root.getAbsolutePath() + "]完成");
      } else {
         File[] list = root.listFiles(this);
         if (list != null)
            for (File file : list) {
               processLicenseHeader(file, charset, headerBody);
            }
      }
   }

   @Override
   public boolean accept(File file) {
      return file.isDirectory() || file.getName().endsWith(".java");
   }

   @Test
   public void test() throws IOException {
      processLicenseHeader(new File("src/main/java"), new FileInputStream("license.txt"), "UTF-8");
   }
}
阅读更多

扫码向博主提问

蒋固金

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • java
  • oracle
  • js
去开通我的Chat快问
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/jianggujin/article/details/81483953
文章标签: License
个人分类: JAVA
上一篇JAVA字节数组转十六进制字符串
下一篇JCodec用法
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭