工具类(文件删除,关于MD5加密,时间工具类)

关于删除文件夹或者文件。

    public static void deleteFile1(File file) {
        // 判断是否存在文件
        if (file.exists()) {
            // 判断是否文件
            if (file.isFile()) {
                file.delete();
                // 判断是否文件夹
            } else if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (File filet : files) {
                    // 递归删除
                    deleteFile1(filet);
                }
            }
        } else {
            LogUtil.debug("删除的文件不存在");
        }
    }
    public static void main(String[] args) {
        deleteFile1(new File("D:\\Users\\ex-ruanzhijun001\\Desktop\\tsss.sql"));
    }

关于MD5加密:MD5的全称是Message-Digest Algorithm 5(信息-摘要算法)

public class MD5Generator {
public static String getMD5(String source, String coding) { 
String s = "";
try {
s = getMD5(source.toString().getBytes(coding));
} catch (UnsupportedEncodingException e) {
LogUtil.error("getMD5(str)发生异常!"+e);
}
return s;
}

public static String getMD5(byte[] source) {  
        String s = null;  
        char hexDigits[] = { // 用来将字节转换成 16 进制表示的字符  
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};  
        try {  
            MessageDigest md = MessageDigest.getInstance("MD5");  
            md.update(source);  
            byte tmp[] = md.digest();          // MD5 的计算结果是一个 128 位的长整数,  
            // 用字节表示就是 16 个字节  
            char str[] = new char[16 * 2];   // 每个字节用 16 进制表示的话,使用两个字符,  
            // 所以表示成 16 进制需要 32 个字符  
            int k = 0;                                // 表示转换结果中对应的字符位置  
            for (int i = 0; i < 16; i++) {    // 从第一个字节开始,对 MD5 的每一个字节  
                // 转换成 16 进制字符的转换  
                byte byte0 = tmp[i];  // 取第 i 个字节  
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];  // 取字节中高 4 位的数字转换,  
                // >>> 为逻辑右移,将符号位一起右移  
                str[k++] = hexDigits[byte0 & 0xf];   // 取字节中低 4 位的数字转换  
            }  
            s = new String(str);  // 换后的结果转换为字符串  
        } catch (Exception e) {  
        LogUtil.error("getMD5(byte)发生异常!"+e);
        }  
        return s;  
    } 


private static final char[] hexCode = "0123456789abcdef".toCharArray();


   public static String generateValue() {
       return generateValue(UUID.randomUUID().toString());
   }


   public static String toHexString(byte[] data) {
       if (data == null) {
           return null;
       }
       StringBuilder r = new StringBuilder(data.length * 2);
       for (byte b : data) {
           r.append(hexCode[(b >> 4) & 0xF]);
           r.append(hexCode[(b & 0xF)]);
       }
       return r.toString();
   }


   public static String generateValue(String param) {
       try {
           MessageDigest algorithm = MessageDigest.getInstance("MD5");
           algorithm.reset();
           algorithm.update(param.getBytes());
           byte[] messageDigest = algorithm.digest();
           return toHexString(messageDigest);
       } catch (NoSuchAlgorithmException e) {
           throw new RuntimeException();
       }
   }
   
   public static void main(String[] args) throws UnsupportedEncodingException {    
System.out.println(MD5Generator.getMD5("ABC".getBytes("UTF-8")));
System.out.println(MD5Generator.getMD5("ABC".getBytes()));
System.out.println(MD5Generator.generateValue("ABC"));
}

}

关于时间工具类:

/**
* 将字符串转成时间格式
* date为时间格式  
* 例如:2016-08-07 16:00:59 yyyy-MM-dd HH:mm:ss

*/
public static Date stringToDate(String date, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(date);
} catch (ParseException e) {
// e.printStackTrace();
}
return null;

}

/**
* 获取当前时间

* @return
*/
public static String nowdateToString(String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(currentDate());
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值