继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java简单算法笔记

qq_童年_1
关注TA
已关注
手记 11
粉丝 42
获赞 175

其实这两个算法也是我从网上找的,做个简单的笔记而已,防止自己遗忘

1.比较App版本号大小

/**
 * 比较版本号的大小,前者大则返回一个正数,后者大返回一个负数,相等则返回0
 * @param version1
 * @param version2
 * @return
 */
public static int compareVersion(String version1, String version2) throws Exception {
    if (version1 == null || version2 == null) {
        throw new Exception("参数不能为空");
    }
    String[ ] versionArray1 = version1.split("\\.");//注意此处为正则匹配,不能用".";
    String[ ] versionArray2 = version2.split("\\.");
    int idx = 0;
    int minLength = Math.min(versionArray1.length, versionArray2.length);//取最小长度值
    int diff = 0;
    while (idx < minLength
            && (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0//先比较长度
            && (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) {//再比较字符
        ++idx;
    }
    //如果已经分出大小,则直接返回,如果未分出大小,则再比较位数,有子版本的为大;
    diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length;
    return diff;
}

2.对文件进行MD5加密

/**
 * MD5文件加密
 * @author Devin
 */
public class MD5Utils {

    protected static char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};  
    protected static MessageDigest messageDigest = null;

    static{  
        try{  
            // 拿到一个MD5转换器(如果想要SHA1参数换成”SHA1”)
            messageDigest = MessageDigest.getInstance("MD5");  
        }catch (NoSuchAlgorithmException e) {  
            System.err.println(MD5Utils.class.getName()+"初始化失败,MessageDigest不支持MD5Util.");  
            e.printStackTrace();  
        }  
    }

    private static void appendHexPair(byte bt, StringBuffer stringbuffer) {  
        char c0 = hexDigits[(bt & 0xf0) >> 4];  
        char c1 = hexDigits[bt & 0xf];  
        stringbuffer.append(c0);  
        stringbuffer.append(c1);  
     } 

    private static String bufferToHex(byte bytes[], int m, int n) {  
        StringBuffer stringbuffer = new StringBuffer(2 * n);  
        int k = m + n;  
        for (int l = m; l < k; l++) {  
         appendHexPair(bytes[l], stringbuffer);  
        }  
        return stringbuffer.toString();  
     }  

    private static String bufferToHex(byte bytes[]) {  
        return bufferToHex(bytes, 0, bytes.length);  
    }

    // 文件MD5加密
    public static String fileMD5(String inputFile) throws IOException {
        // 缓冲区大小(这个可以抽出一个参数)
        int bufferSize = 256 * 1024;
        FileInputStream fileInputStream = null;
        DigestInputStream digestInputStream = null;
        try {
           // 使用DigestInputStream
           fileInputStream = new FileInputStream(inputFile);
           digestInputStream = new DigestInputStream(fileInputStream,messageDigest);
           // read的过程中进行MD5处理,直到读完文件
           byte[ ] buffer =new byte[bufferSize];
           while (digestInputStream.read(buffer) > 0);
           // 获取最终的MessageDigest
           messageDigest= digestInputStream.getMessageDigest();
           // 拿到结果,也是字节数组,包含16个元素
           byte[ ] resultByteArray = messageDigest.digest();
           // 同样,把字节数组转换成字符串
           return bufferToHex(resultByteArray);
        } finally {
           try {
              digestInputStream.close();
           } catch (Exception e) {
           }
           try {
              fileInputStream.close();
           } catch (Exception e) {
           }
        }
     }

}
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP