咱們九天鳥的p2p網貸系統,基本算是開發完成了。
如今,想給後端的Java代碼,增長版權信息。
手動去copy-paste,太沒有技術含量。
因而,寫了個Java工具類,給Java源文件增長版權信息。
源代碼,開頭的註釋,就是版權描述。詳細實現,就看代碼吧。
java
/* *Copyright © 2015 JiuTianNiao *武漢九天鳥科技有限公司 *http://JiuTianNiao.com *All rights reserved. */ package com.jiutianniao.p2p.base.util; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import org.apache.log4j.Logger; //給java文件,增長版權信息 public class CopyrightUtil { private static Logger log = Logger.getLogger(CopyrightUtil.class); public static void main(String[] args) throws Exception { //java文件所在目錄 String dir = "E:/GitRepository/funds"; File file = new File(dir); addCopyright4Directory(file); } public static void addCopyright4Directory(File file) throws Exception { File[] files = file.listFiles(); if (files == null || files.length == 0){ return; } for (File f : files) { if (f.isFile()) { addCopyright4File(f); System.out.println("文件===" + f.getName()); } else { System.out.println("目錄==" + f.getName()); addCopyright4Directory(f); } } } public static void addCopyright4File(File file) throws Exception { String fileName = file.getName(); boolean isJava = fileName.endsWith(".java"); if (!isJava) { log.info("This file is not java source file,filaName=" + fileName); return; } if (isJava) { // 版權字符串 String copyright = "/*\n *Copyright © 2015 JiuTianNiao\n " + "*武漢九天鳥科技有限公司\n " + "*http://JiuTianNiao.com\n " + "*All rights reserved.\n */\n"; //嘗試使用了RandomAccessFile.writeUTF,問題是開頭字符是「NUL」,沒能解決。 BufferedReader br = new BufferedReader(new FileReader(file)); String line; String content = ""; //讀取一行,必定要加上「換行符」,Windows下能夠直接用「\n」 String lineSeperator ="\n"; //lineSeperator = System.getProperty("line.separator") while ((line = br.readLine()) != null) { content += line + lineSeperator; } br.close(); //把拼接後的字符串寫回去 FileWriter fileWriter = new FileWriter(file); fileWriter.write(copyright); fileWriter.write(content); fileWriter.close(); } } }
武漢九天鳥-p2p網貸系統開發-互聯網應用軟件開發apache
公司官網:http://jiutianniao.com後端
社交問答:http://ask.jiutianniao.comdom