国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

java中常用工具類(lèi)之字符串操作類(lèi)和MD5加密解密類(lèi)

2019-11-26 15:28:47
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

java中常用的工具類(lèi)之String和MD5加密解密類(lèi)

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

一、String工具類(lèi)

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)操作輔助類(lèi)。 *  * @author 宋立君 * @date 2014年06月24日 */public class FileUtil {	private static final String FOLDER_SEPARATOR = "/";	private static final char EXTENSION_SEPARATOR = '.';	/**	 * 功能:復(fù)制文件或者文件夾。	 * 	 * @author 宋立君	 * @date 2014年06月24日	 * @param inputFile	 *      源文件	 * @param outputFile	 *      目的文件	 * @param isOverWrite	 *      是否覆蓋(只針對(duì)文件)	 * @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 {		// 是個(gè)文件。		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單個(gè)文件	 * 	 * @author 宋立君	 * @date 2014年06月24日	 * @param inputFile	 *      源文件	 * @param outputFile	 *      目標(biāo)文件	 * @param isOverWrite	 *      是否允許覆蓋	 * @throws IOException	 */	private static void copySimpleFile(File inputFile, File outputFile,			boolean isOverWrite) throws IOException {		// 目標(biāo)文件已經(jīng)存在		if (outputFile.exists()) {			if (isOverWrite) {				if (!outputFile.delete()) {					throw new RuntimeException(outputFile.getPath() + "無(wú)法覆蓋!");				}			} 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();	}	/**	 * 從文件路徑中抽取文件的擴(kuò)展名, 例如. "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 抽取出來(lái)的文件名, 如果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工具類(lèi)

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)操作輔助類(lèi)。 *  * @author 宋立君 * @date 2014年06月24日 */public class FileUtil {	private static final String FOLDER_SEPARATOR = "/";	private static final char EXTENSION_SEPARATOR = '.';	/**	 * 功能:復(fù)制文件或者文件夾。	 * 	 * @author 宋立君	 * @date 2014年06月24日	 * @param inputFile	 *      源文件	 * @param outputFile	 *      目的文件	 * @param isOverWrite	 *      是否覆蓋(只針對(duì)文件)	 * @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 {		// 是個(gè)文件。		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單個(gè)文件	 * 	 * @author 宋立君	 * @date 2014年06月24日	 * @param inputFile	 *      源文件	 * @param outputFile	 *      目標(biāo)文件	 * @param isOverWrite	 *      是否允許覆蓋	 * @throws IOException	 */	private static void copySimpleFile(File inputFile, File outputFile,			boolean isOverWrite) throws IOException {		// 目標(biāo)文件已經(jīng)存在		if (outputFile.exists()) {			if (isOverWrite) {				if (!outputFile.delete()) {					throw new RuntimeException(outputFile.getPath() + "無(wú)法覆蓋!");				}			} 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();	}	/**	 * 從文件路徑中抽取文件的擴(kuò)展名, 例如. "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 抽取出來(lái)的文件名, 如果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();	}}

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 金溪县| 手机| 南华县| 沙湾县| 芦溪县| 宽城| 遂平县| 七台河市| 容城县| 佳木斯市| 眉山市| 安达市| 莒南县| 太康县| 长子县| 井冈山市| 宝山区| 太原市| 宣威市| 揭东县| 确山县| 三台县| 元江| 峨眉山市| 来宾市| 金昌市| 花垣县| 甘南县| 浮山县| 古丈县| 东光县| 铁岭县| 北京市| 保山市| 桃园市| 平武县| 安岳县| 隆尧县| 海淀区| 奉节县| 兴隆县|