1. 程式人生 > >JAVA程式碼新增License

JAVA程式碼新增License

在開原始碼的時候,我們經常會在程式碼頂部新增License資訊,每個檔案複製貼上顯然是比較麻煩的,我們可以在工具中進行配置,在建立新的類的時候自動為我們新增相關資訊,以eclipse為例。

進入Preference->Java->Code Style->Code Template
這裡寫圖片描述

在中間的面板中選擇Comments->Files,然後單擊Edit...按鈕,在彈出的對話方塊中填寫我們的License資訊。
這裡寫圖片描述

最後在我們建立新的JAVA類的時候需要勾選Generate comments

這裡寫圖片描述

這樣,我們新建立的檔案就會自動新增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"); } }