Copyright4RZ.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright [2020] [MaxKey of copyright http://www.maxkey.top ]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.maxkey;
  17. import java.io.BufferedReader;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.FileNotFoundException;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.InputStreamReader;
  24. import java.io.OutputStreamWriter;
  25. import java.io.UnsupportedEncodingException;
  26. /**
  27. * 给java文件批量添加License信息.
  28. * @author MaxKey Copyright Adder
  29. *
  30. */
  31. public class Copyright4RZ {
  32. // 存放java文件的文件夹,必须是文件夹
  33. private static String srcFolder = "D:\\MaxKey\\Workspaces\\maxkey\\MaxKey\\maxkey-webs\\maxkey-web-mgt";
  34. //已添加标识
  35. private static String copyRightText = "http://www.apache.org/licenses/LICENSE-2.0";
  36. //扫描目录
  37. private String folder;
  38. //待添加所以文件统计
  39. private long fileCount = 0;
  40. //添加的问题就统计
  41. private long copyRightFileCount = 0;
  42. private static String lineSeperator = System.getProperty("line.separator");
  43. private static String encode = "UTF-8";
  44. private static OutputStreamWriter writer;
  45. static {
  46. try {
  47. writer = new OutputStreamWriter(new FileOutputStream("D:/MaxKey/code.txt"), encode);
  48. } catch (UnsupportedEncodingException e) {
  49. e.printStackTrace();
  50. } catch (FileNotFoundException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. /**
  55. * Copyright.
  56. * @param folder java文件夹.
  57. * @param copyRight 版权内容.
  58. */
  59. public Copyright4RZ(String folder, String copyRight) {
  60. this.folder = folder;
  61. }
  62. /**
  63. * main .
  64. * @param args String
  65. * @throws IOException IOException
  66. */
  67. public static void main(String[] args) throws IOException {
  68. // 从文件读取版权内容
  69. // 在D盘创建一个copyright.txt文件,把版权内容放进去即可
  70. String copyright = readCopyrightFromFile(
  71. Copyright4RZ.class.getResource("copyright.txt").getFile());
  72. new Copyright4RZ(srcFolder, copyright).process();
  73. writer.close();
  74. }
  75. /**
  76. * process.
  77. * @throws IOException not
  78. */
  79. public void process() throws IOException {
  80. this.addCopyright(new File(folder));
  81. System.out.println("fileCount " + fileCount);
  82. System.out.println("copyRightFileCount " + copyRightFileCount);
  83. }
  84. private void addCopyright(File folder) throws IOException {
  85. File[] files = folder.listFiles();
  86. if (files == null || files.length == 0) {
  87. return;
  88. }
  89. for (File f : files) {
  90. if (f.isFile()) {
  91. doAddCopyright(f);
  92. } else {
  93. addCopyright(f);
  94. }
  95. }
  96. }
  97. private void doAddCopyright(File file) throws IOException {
  98. String fileName = file.getName();
  99. boolean isJavaFile = fileName.toLowerCase().endsWith(".java");
  100. //boolean isJavaFile = fileName.toLowerCase().endsWith(".ftl");
  101. this.fileCount++;
  102. if (isJavaFile) {
  103. copyRightFileCount++;
  104. System.out.println(file.getAbsolutePath());
  105. try {
  106. this.doWrite(file);
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. }
  112. private void doWrite(File file) throws IOException {
  113. StringBuilder javaFileContent = new StringBuilder();
  114. String line = null;
  115. boolean isAddCopyrightFile = isAddCopyrightFile(file.getAbsolutePath());
  116. // 先添加copyright到文件头
  117. //javaFileContent.append(copyRight).append(lineSeperator);
  118. // 追加剩余内容
  119. BufferedReader br = new BufferedReader(
  120. new InputStreamReader(new FileInputStream(file), encode));
  121. int i=0;
  122. while ((line = br.readLine()) != null) {
  123. if(isAddCopyrightFile && i< 16) {
  124. i++;
  125. continue;
  126. }
  127. if(line.equals("")
  128. ||line.replaceAll(" ", "").equals("")
  129. ||line.replaceAll("\t", "").equals("")
  130. ) {
  131. }else {
  132. javaFileContent.append(line).append(lineSeperator);
  133. }
  134. }
  135. //OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), encode);
  136. writer.write(javaFileContent.toString());
  137. br.close();
  138. }
  139. private static String readCopyrightFromFile(String copyFilePath) throws IOException {
  140. StringBuilder copyright = new StringBuilder();
  141. String line = null;
  142. BufferedReader br = new BufferedReader(
  143. new InputStreamReader(new FileInputStream(copyFilePath), encode));
  144. while ((line = br.readLine()) != null) {
  145. copyright.append(line).append(lineSeperator);
  146. }
  147. br.close();
  148. return copyright.toString();
  149. }
  150. private static boolean isAddCopyrightFile(String filePath) throws IOException {
  151. boolean isAddCopyright = false;
  152. String line = null;
  153. BufferedReader br = new BufferedReader(
  154. new InputStreamReader(new FileInputStream(filePath), encode));
  155. while ((line = br.readLine()) != null) {
  156. if (line.indexOf(copyRightText) > -1) {
  157. isAddCopyright = true;
  158. break;
  159. }
  160. }
  161. br.close();
  162. return isAddCopyright;
  163. }
  164. }