java中常用工具类之字符串操作类和MD5加密解密类

 更新时间:2014年07月05日 12:59:34   投稿:junjie  
这篇文章主要介绍了java中常用工具类之字符串操作类和MD5加密解密类,需要的朋友可以参考下

java中常用的工具类之String和MD5加密解密类

我们java程序员在开发项目的是常常会用到一些工具类。今天我分享一下我的两个工具类,大家可以在项目中使用。

一、String工具类

package com.itjh.javaUtil;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 文件相关操作辅助类。
 * 
 * @author 宋立君
 * @date 2014年06月24日
 */
public class FileUtil {
	private static final String FOLDER_SEPARATOR = "/";
	private static final char EXTENSION_SEPARATOR = '.';

	/**
	 * 功能:复制文件或者文件夹。
	 * 
	 * @author 宋立君
	 * @date 2014年06月24日
	 * @param inputFile
	 *      源文件
	 * @param outputFile
	 *      目的文件
	 * @param isOverWrite
	 *      是否覆盖(只针对文件)
	 * @throws IOException
	 */
	public static void copy(File inputFile, File outputFile, boolean isOverWrite)
			throws IOException {
		if (!inputFile.exists()) {
			throw new RuntimeException(inputFile.getPath() + "源目录不存在!");
		}
		copyPri(inputFile, outputFile, isOverWrite);
	}

	/**
	 * 功能:为copy 做递归使用。
	 * 
	 * @author 宋立君
	 * @date 2014年06月24日
	 * @param inputFile
	 * @param outputFile
	 * @param isOverWrite
	 * @throws IOException
	 */
	private static void copyPri(File inputFile, File outputFile,
			boolean isOverWrite) throws IOException {
		// 是个文件。
		if (inputFile.isFile()) {
			copySimpleFile(inputFile, outputFile, isOverWrite);
		} else {
			// 文件夹
			if (!outputFile.exists()) {
				outputFile.mkdir();
			}
			// 循环子文件夹
			for (File child : inputFile.listFiles()) {
				copy(child,
						new File(outputFile.getPath() + "/" + child.getName()),
						isOverWrite);
			}
		}
	}

	/**
	 * 功能:copy单个文件
	 * 
	 * @author 宋立君
	 * @date 2014年06月24日
	 * @param inputFile
	 *      源文件
	 * @param outputFile
	 *      目标文件
	 * @param isOverWrite
	 *      是否允许覆盖
	 * @throws IOException
	 */
	private static void copySimpleFile(File inputFile, File outputFile,
			boolean isOverWrite) throws IOException {
		// 目标文件已经存在
		if (outputFile.exists()) {
			if (isOverWrite) {
				if (!outputFile.delete()) {
					throw new RuntimeException(outputFile.getPath() + "无法覆盖!");
				}
			} else {
				// 不允许覆盖
				return;
			}
		}
		InputStream in = new FileInputStream(inputFile);
		OutputStream out = new FileOutputStream(outputFile);
		byte[] buffer = new byte[1024];
		int read = 0;
		while ((read = in.read(buffer)) != -1) {
			out.write(buffer, 0, read);
		}
		in.close();
		out.close();
	}

	/**
	 * 功能:删除文件
	 * 
	 * @author 宋立君
	 * @date 2014年06月24日
	 * @param file
	 *      文件
	 */
	public static void delete(File file) {
		deleteFile(file);
	}

	/**
	 * 功能:删除文件,内部递归使用
	 * 
	 * @author 宋立君
	 * @date 2014年06月24日
	 * @param file
	 *      文件
	 * @return boolean true 删除成功,false 删除失败。
	 */
	private static void deleteFile(File file) {
		if (file == null || !file.exists()) {
			return;
		}
		// 单文件
		if (!file.isDirectory()) {
			boolean delFlag = file.delete();
			if (!delFlag) {
				throw new RuntimeException(file.getPath() + "删除失败!");
			} else {
				return;
			}
		}
		// 删除子目录
		for (File child : file.listFiles()) {
			deleteFile(child);
		}
		// 删除自己
		file.delete();
	}

	/**
	 * 从文件路径中抽取文件的扩展名, 例如. "mypath/myfile.txt" -> "txt". * @author 宋立君
	 * 
	 * @date 2014年06月24日
	 * @param 文件路径
	 * @return 如果path为null,直接返回null。
	 */
	public static String getFilenameExtension(String path) {
		if (path == null) {
			return null;
		}
		int extIndex = path.lastIndexOf(EXTENSION_SEPARATOR);
		if (extIndex == -1) {
			return null;
		}
		int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR);
		if (folderIndex > extIndex) {
			return null;
		}
		return path.substring(extIndex + 1);
	}

	/**
	 * 从文件路径中抽取文件名, 例如: "mypath/myfile.txt" -> "myfile.txt"。 * @author 宋立君
	 * 
	 * @date 2014年06月24日
	 * @param path
	 *      文件路径。
	 * @return 抽取出来的文件名, 如果path为null,直接返回null。
	 */
	public static String getFilename(String path) {
		if (path == null) {
			return null;
		}
		int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR);
		return (separatorIndex != -1 ? path.substring(separatorIndex + 1)
				: path);
	}

	/**
	 * 功能:保存文件。
	 * 
	 * @author 宋立君
	 * @date 2014年06月24日
	 * @param content
	 *      字节
	 * @param file
	 *      保存到的文件
	 * @throws IOException
	 */
	public static void save(byte[] content, File file) throws IOException {
		if (file == null) {
			throw new RuntimeException("保存文件不能为空");
		}
		if (content == null) {
			throw new RuntimeException("文件流不能为空");
		}
		InputStream is = new ByteArrayInputStream(content);
		save(is, file);
	}

	/**
	 * 功能:保存文件
	 * 
	 * @author 宋立君
	 * @date 2014年06月24日
	 * @param streamIn
	 *      文件流
	 * @param file
	 *      保存到的文件
	 * @throws IOException
	 */
	public static void save(InputStream streamIn, File file) throws IOException {
		if (file == null) {
			throw new RuntimeException("保存文件不能为空");
		}
		if (streamIn == null) {
			throw new RuntimeException("文件流不能为空");
		}
		// 输出流
		OutputStream streamOut = null;
		// 文件夹不存在就创建。
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		streamOut = new FileOutputStream(file);
		int bytesRead = 0;
		byte[] buffer = new byte[8192];
		while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
			streamOut.write(buffer, 0, bytesRead);
		}
		streamOut.close();
		streamIn.close();
	}
}

二、MD5工具类

package com.itjh.javaUtil;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 文件相关操作辅助类。
 * 
 * @author 宋立君
 * @date 2014年06月24日
 */
public class FileUtil {
	private static final String FOLDER_SEPARATOR = "/";
	private static final char EXTENSION_SEPARATOR = '.';

	/**
	 * 功能:复制文件或者文件夹。
	 * 
	 * @author 宋立君
	 * @date 2014年06月24日
	 * @param inputFile
	 *      源文件
	 * @param outputFile
	 *      目的文件
	 * @param isOverWrite
	 *      是否覆盖(只针对文件)
	 * @throws IOException
	 */
	public static void copy(File inputFile, File outputFile, boolean isOverWrite)
			throws IOException {
		if (!inputFile.exists()) {
			throw new RuntimeException(inputFile.getPath() + "源目录不存在!");
		}
		copyPri(inputFile, outputFile, isOverWrite);
	}

	/**
	 * 功能:为copy 做递归使用。
	 * 
	 * @author 宋立君
	 * @date 2014年06月24日
	 * @param inputFile
	 * @param outputFile
	 * @param isOverWrite
	 * @throws IOException
	 */
	private static void copyPri(File inputFile, File outputFile,
			boolean isOverWrite) throws IOException {
		// 是个文件。
		if (inputFile.isFile()) {
			copySimpleFile(inputFile, outputFile, isOverWrite);
		} else {
			// 文件夹
			if (!outputFile.exists()) {
				outputFile.mkdir();
			}
			// 循环子文件夹
			for (File child : inputFile.listFiles()) {
				copy(child,
						new File(outputFile.getPath() + "/" + child.getName()),
						isOverWrite);
			}
		}
	}

	/**
	 * 功能:copy单个文件
	 * 
	 * @author 宋立君
	 * @date 2014年06月24日
	 * @param inputFile
	 *      源文件
	 * @param outputFile
	 *      目标文件
	 * @param isOverWrite
	 *      是否允许覆盖
	 * @throws IOException
	 */
	private static void copySimpleFile(File inputFile, File outputFile,
			boolean isOverWrite) throws IOException {
		// 目标文件已经存在
		if (outputFile.exists()) {
			if (isOverWrite) {
				if (!outputFile.delete()) {
					throw new RuntimeException(outputFile.getPath() + "无法覆盖!");
				}
			} else {
				// 不允许覆盖
				return;
			}
		}
		InputStream in = new FileInputStream(inputFile);
		OutputStream out = new FileOutputStream(outputFile);
		byte[] buffer = new byte[1024];
		int read = 0;
		while ((read = in.read(buffer)) != -1) {
			out.write(buffer, 0, read);
		}
		in.close();
		out.close();
	}

	/**
	 * 功能:删除文件
	 * 
	 * @author 宋立君
	 * @date 2014年06月24日
	 * @param file
	 *      文件
	 */
	public static void delete(File file) {
		deleteFile(file);
	}

	/**
	 * 功能:删除文件,内部递归使用
	 * 
	 * @author 宋立君
	 * @date 2014年06月24日
	 * @param file
	 *      文件
	 * @return boolean true 删除成功,false 删除失败。
	 */
	private static void deleteFile(File file) {
		if (file == null || !file.exists()) {
			return;
		}
		// 单文件
		if (!file.isDirectory()) {
			boolean delFlag = file.delete();
			if (!delFlag) {
				throw new RuntimeException(file.getPath() + "删除失败!");
			} else {
				return;
			}
		}
		// 删除子目录
		for (File child : file.listFiles()) {
			deleteFile(child);
		}
		// 删除自己
		file.delete();
	}

	/**
	 * 从文件路径中抽取文件的扩展名, 例如. "mypath/myfile.txt" -> "txt". * @author 宋立君
	 * 
	 * @date 2014年06月24日
	 * @param 文件路径
	 * @return 如果path为null,直接返回null。
	 */
	public static String getFilenameExtension(String path) {
		if (path == null) {
			return null;
		}
		int extIndex = path.lastIndexOf(EXTENSION_SEPARATOR);
		if (extIndex == -1) {
			return null;
		}
		int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR);
		if (folderIndex > extIndex) {
			return null;
		}
		return path.substring(extIndex + 1);
	}

	/**
	 * 从文件路径中抽取文件名, 例如: "mypath/myfile.txt" -> "myfile.txt"。 * @author 宋立君
	 * 
	 * @date 2014年06月24日
	 * @param path
	 *      文件路径。
	 * @return 抽取出来的文件名, 如果path为null,直接返回null。
	 */
	public static String getFilename(String path) {
		if (path == null) {
			return null;
		}
		int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR);
		return (separatorIndex != -1 ? path.substring(separatorIndex + 1)
				: path);
	}

	/**
	 * 功能:保存文件。
	 * 
	 * @author 宋立君
	 * @date 2014年06月24日
	 * @param content
	 *      字节
	 * @param file
	 *      保存到的文件
	 * @throws IOException
	 */
	public static void save(byte[] content, File file) throws IOException {
		if (file == null) {
			throw new RuntimeException("保存文件不能为空");
		}
		if (content == null) {
			throw new RuntimeException("文件流不能为空");
		}
		InputStream is = new ByteArrayInputStream(content);
		save(is, file);
	}

	/**
	 * 功能:保存文件
	 * 
	 * @author 宋立君
	 * @date 2014年06月24日
	 * @param streamIn
	 *      文件流
	 * @param file
	 *      保存到的文件
	 * @throws IOException
	 */
	public static void save(InputStream streamIn, File file) throws IOException {
		if (file == null) {
			throw new RuntimeException("保存文件不能为空");
		}
		if (streamIn == null) {
			throw new RuntimeException("文件流不能为空");
		}
		// 输出流
		OutputStream streamOut = null;
		// 文件夹不存在就创建。
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		streamOut = new FileOutputStream(file);
		int bytesRead = 0;
		byte[] buffer = new byte[8192];
		while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
			streamOut.write(buffer, 0, bytesRead);
		}
		streamOut.close();
		streamIn.close();
	}
}

相关文章

  • 一文带你弄懂Java中线程池的原理

    一文带你弄懂Java中线程池的原理

    工作中,我们经常使用线程池,但是你真的了解线程池的原理吗?同时,线程池工作原理和底层实现原理也是面试经常问的考题,所以,今天我们一起聊聊线程池的原理吧
    2022-12-12
  • 基于Ant路径匹配规则AntPathMatcher的注意事项

    基于Ant路径匹配规则AntPathMatcher的注意事项

    这篇文章主要介绍了基于Ant路径匹配规则AntPathMatcher的注意事项,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • PowerJob的ProcessorLoader工作流程源码解读

    PowerJob的ProcessorLoader工作流程源码解读

    这篇文章主要为大家介绍了PowerJob的ProcessorLoader工作流程源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • httpclient staleConnectionCheckEnabled获取连接流程解析

    httpclient staleConnectionCheckEnabled获取连接流程解析

    这篇文章主要为大家介绍了httpclient staleConnectionCheckEnabled获取连接流程示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • Hadoop Combiner使用方法详解

    Hadoop Combiner使用方法详解

    这篇文章主要介绍了 Hadoop Combiner使用方法详解的相关资料,希望通过本文能帮助到大家让大家理解掌握这部分内容,需要的朋友可以参考下
    2017-10-10
  • Spring整合JPA与Hibernate流程详解

    Spring整合JPA与Hibernate流程详解

    这篇文章主要介绍了Spring整合Hibernate与JPA,在正式进入Hibernate的高级应用之前,需要了解声明是数据模型与领域模型,这两个概念将会帮助我们更好的理解实体对象的关联关系映射
    2023-01-01
  • 详解SpringCloud微服务架构之Hystrix断路器

    详解SpringCloud微服务架构之Hystrix断路器

    本篇文章主要介绍了详解SpringCloud微服务架构之Hystrix断路器,Hystrix是一个库,通过添加延迟容差和容错逻辑来帮助您控制这些分布式服务之间的交互,有兴趣的可以了解一下
    2018-01-01
  • Redis实现商品秒杀功能页面流程

    Redis实现商品秒杀功能页面流程

    这篇文章主要介绍了Redis实现商品秒杀功能的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-09-09
  • Jenkins Pipline实现及原理示例解析

    Jenkins Pipline实现及原理示例解析

    这篇文章主要为大家介绍了Jenkins Pipline实现及原理示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • 使用JWT作为Spring Security OAuth2的token存储问题

    使用JWT作为Spring Security OAuth2的token存储问题

    这篇文章主要介绍了使用JWT作为Spring Security OAuth2的token存储,大家经常使用的方法有两种一种是使用JWT作为Token传递,一种是使用Redis存储Token,资源服务器本地访问Redis校验Token,需要的朋友可以参考下
    2021-12-12

最新评论