java licence header_如何给JAVA源代码文件统一地添加licence信息头

展开全部

下面就直接上代码吧!当然懂得就跳过。

/**

* Licensed to the Apache Software Foundation (ASF) under one or more

* contributor license agreements. See the NOTICE file distributed with

* this work for additional information regarding copyright ownership.

* The ASF licenses this file to You under the Apache License, Version 2.0

* (the "License"); you may not use this file except in compliance with

* the License. You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package com.apdplat.platform.util;

import java.io.*;

import java.util.ArrayList;

import java.util.List;

/**

* 给JAVA源代码文e69da5e6ba9062616964757a686964616f31333337386637件统一地添加licence信息头

* 检查文件package、import、类级别注释、是否有public class

* 用到了Java7的新特性,强大

* @author ysc

*/

public class AddLicenceForJavaFile {

private static int count = 0;

private static List fail = new ArrayList<>();

private static List wrong = new ArrayList<>();

public static void main(String[] args) {

String licence="/**\n" +

" * Licensed to the Apache Software Foundation (ASF) under one or more\n" +

" * contributor license agreements. See the NOTICE file distributed with\n" +

" * this work for additional information regarding copyright ownership.\n" +

" * The ASF licenses this file to You under the Apache License, Version 2.0\n" +

" * (the \"License\"); you may not use this file except in compliance with\n" +

" * the License. You may obtain a copy of the License at\n" +

" *\n" +

" * http://www.apache.org/licenses/LICENSE-2.0\n" +

" *\n" +

" * Unless required by applicable law or agreed to in writing, software\n" +

" * distributed under the License is distributed on an \"AS IS\" BASIS,\n" +

" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +

" * See the License for the specific language governing permissions and\n" +

" * limitations under the License.\n" +

" */";

addLicenceForJavaFile(new File("D:\\Workspaces\\NetBeansProjects\\APDPlat"),licence);

System.out.println("为 "+count+" 个Java源代码文件添加licence信息头");

if(fail.size()>0){

System.out.println("处理失败个数 "+fail.size());

for(String f : fail){

System.out.println(" "+f);

}

}

if(wrong.size()>0){

System.out.println("JAVA源代码错误个数 "+wrong.size());

for(String w : wrong){

System.out.println(" "+w);

}

}

}

/**

* 给JAVA源代码文件统一地添加licence信息头

* @param path 源码所处的根目录

* @param licence 许可证信息(在netbeans中复制一段文本粘贴到变量的双引号内,IDE自动格式化,相当赞)

*/

private static void addLicenceForJavaFile(File path, String licence) {

if (path != null && path.exists()) {

//处理文件夹

if (path.isDirectory()) {

String[] children = path.list();

for (int i = 0; i < children.length; i++) {

File child = new File(path.getPath() + System.getProperty("file.separator") + children[i]);

//递归处理

addLicenceForJavaFile(child, licence);

}

} else {

//处理java文件

if (path.getName()。toLowerCase()。endsWith(".java")) {

System.out.println(path.getAbsolutePath());

count++;

try {

byte[] content;

try (RandomAccessFile f = new RandomAccessFile(path, "rw")) {

content = new byte[ (int) f.length()];

f.readFully(content);

}

String text = new String(content);

text = text.trim();

while (text.startsWith("/n")) {

text = text.substring(1);

}

//如果已经有同样的licence,则忽略

int pos = text.indexOf(licence);

if(pos!=-1){

return;

}

//有package声明的,保留package以后的内容

if (text.indexOf("package") != -1) {

text = text.substring(text.indexOf("package"));

}

//没有package声明的,有import声明的,保留import以后的内容

else if (text.indexOf("package") == -1 && text.indexOf("import") != -1) {

text = text.substring(text.indexOf("import"));

}

//没有package声明也没有import声明的,有类级别注释的,则保留类级别注释以后的内容

else if (text.indexOf("package") == -1 && text.indexOf("import") == -1 && text.indexOf("/**") != -1 && text.indexOf("public class") != -1 && text.indexOf("/**")

text = text.substring(text.indexOf("/**"));

}

//没有package声明也没有import声明的,也没有类级别注释的则保留public class以后的内容

else if (text.indexOf("package") == -1 && text.indexOf("import") == -1 && text.indexOf("public class") != -1 && ( text.indexOf("/**")>text.indexOf("public class") || text.indexOf("/**")==-1 )) {

text = text.substring(text.indexOf("public class"));

}else{

wrong.add(path.getAbsolutePath());

return;

}

try (FileWriter writer = new FileWriter(path)) {

writer.write(licence);

writer.write("\n\n");

writer.write(text);

}

}

catch (Exception ex) {

fail.add(path.getAbsolutePath());

}

}

}

}

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java中使用公钥加密私钥解密原理. KeyGenerater类: public class KeyGenerater { private byte[] priKey; private byte[] pubKey; public void generater() { try { KeyPairGenerator keygen = KeyPairGenerator .getInstance("RSA"); SecureRandom secrand = new SecureRandom(); secrand.setSeed("www.川江号子.cn".getBytes()); // 初始化随机产生器 keygen.initialize(1024, secrand); KeyPair keys = keygen.genKeyPair(); PublicKey pubkey = keys.getPublic(); PrivateKey prikey = keys.getPrivate() pubKey = Base64.encodeToByte(pubkey.getEncoded()); priKey = Base64.encodeToByte(prikey.getEncoded()); System.out.println("pubKey = " + new String(pubKey)); System.out.println("priKey = " + new String(priKey)); } catch (java.lang.Exception e) { System.out.println("生成密钥对失败"); e.printStackTrace(); } } public byte[] getPriKey() { return priKey; } public byte[] getPubKey() { return pubKey; } } Signaturer 类: public class Signaturer { public static byte[] sign(byte[] priKeyText, String plainText) { try { PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decode(priKeyText)); KeyFactory keyf = KeyFactory.getInstance("RSA"); PrivateKey prikey = keyf.generatePrivate(priPKCS8); // 用私钥对信息生成数字签名 Signature signet = java.security.Signature.getInstance("MD5withRSA"); signet.initSign(prikey); signet.update(plainText.getBytes()); byte[] signed = Base64.encodeToByte(signet.sign()); return signed; catch (java.lang.Exception e) { System.out.println("签名失败"); e.printStackTrace(); } return null; } } SignProvider 类: public class SignProvider { private SignProvider() { } public static boolean verify(byte[] pubKeyText, String plainText, byte[] signText) { try { // 解密由base64编码的公钥,并构造X509EncodedKeySpec对象 X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(Base64.decode(pubKeyText)); // RSA对称加密算法 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); // 取公钥匙对象 PublicKey pubKey = keyFactory.generatePublic(bobPubKeySpec); //

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值