最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

我們java程序員在開發(fā)項目的是常常會用到一些工具類。今天我分享一下我的兩個工具類,大家可以在項目中使用。

一、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;

/**
 * 文件相關(guān)操作輔助類。
 * 
 * @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();
			}
			// 循環(huán)子文件夾
			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 {
		// 目標文件已經(jīng)存在
		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);
	}

	/**
	 * 功能:刪除文件,內(nèi)部遞歸使用
	 * 
	 * @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
	 *      字節(jié)
	 * @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;
		// 文件夾不存在就創(chuàng)建。
		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;

/**
 * 文件相關(guān)操作輔助類。
 * 
 * @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();
			}
			// 循環(huán)子文件夾
			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 {
		// 目標文件已經(jīng)存在
		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);
	}

	/**
	 * 功能:刪除文件,內(nèi)部遞歸使用
	 * 
	 * @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
	 *      字節(jié)
	 * @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;
		// 文件夾不存在就創(chuàng)建。
		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();
	}
}

相關(guān)文章

  • 一文帶你弄懂Java中線程池的原理

    一文帶你弄懂Java中線程池的原理

    工作中,我們經(jīng)常使用線程池,但是你真的了解線程池的原理嗎?同時,線程池工作原理和底層實現(xiàn)原理也是面試經(jīng)常問的考題,所以,今天我們一起聊聊線程池的原理吧
    2022-12-12
  • 基于Ant路徑匹配規(guī)則AntPathMatcher的注意事項

    基于Ant路徑匹配規(guī)則AntPathMatcher的注意事項

    這篇文章主要介紹了基于Ant路徑匹配規(guī)則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使用方法詳解的相關(guān)資料,希望通過本文能幫助到大家讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • Spring整合JPA與Hibernate流程詳解

    Spring整合JPA與Hibernate流程詳解

    這篇文章主要介紹了Spring整合Hibernate與JPA,在正式進入Hibernate的高級應用之前,需要了解聲明是數(shù)據(jù)模型與領(lǐng)域模型,這兩個概念將會幫助我們更好的理解實體對象的關(guān)聯(lián)關(guān)系映射
    2023-01-01
  • 詳解SpringCloud微服務架構(gòu)之Hystrix斷路器

    詳解SpringCloud微服務架構(gòu)之Hystrix斷路器

    本篇文章主要介紹了詳解SpringCloud微服務架構(gòu)之Hystrix斷路器,Hystrix是一個庫,通過添加延遲容差和容錯邏輯來幫助您控制這些分布式服務之間的交互,有興趣的可以了解一下
    2018-01-01
  • Redis實現(xiàn)商品秒殺功能頁面流程

    Redis實現(xiàn)商品秒殺功能頁面流程

    這篇文章主要介紹了Redis實現(xiàn)商品秒殺功能的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-09-09
  • Jenkins?Pipline實現(xiàn)及原理示例解析

    Jenkins?Pipline實現(xiàn)及原理示例解析

    這篇文章主要為大家介紹了Jenkins?Pipline實現(xiàn)及原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • 使用JWT作為Spring?Security?OAuth2的token存儲問題

    使用JWT作為Spring?Security?OAuth2的token存儲問題

    這篇文章主要介紹了使用JWT作為Spring?Security?OAuth2的token存儲,大家經(jīng)常使用的方法有兩種一種是使用JWT作為Token傳遞,一種是使用Redis存儲Token,資源服務器本地訪問Redis校驗Token,需要的朋友可以參考下
    2021-12-12

最新評論

大同县| 房产| 申扎县| 临潭县| 扎赉特旗| 突泉县| 齐齐哈尔市| 任丘市| 松溪县| 淮阳县| 西平县| 辰溪县| 岑巩县| 苍梧县| 河北省| 吉隆县| 台湾省| 上高县| 东山县| 石景山区| 鲁甸县| 林州市| 马鞍山市| 游戏| 随州市| 长泰县| 富平县| 诏安县| 松潘县| 静安区| 万源市| 衡水市| 嵊泗县| 长顺县| 沛县| 晋中市| 边坝县| 通辽市| 诸城市| 板桥市| 大城县|