java中常用工具类之字符串操作类和MD5加密解密类
程序员文章站
2024-02-29 13:54:28
java中常用的工具类之string和md5加密解密类
我们java程序员在开发项目的是常常会用到一些工具类。今天我分享一下我的两个工具类,大家可以在项目中使用。
一、...
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常用工具类之DES和Base64加密解密类
下一篇: Java中常见的5种WEB服务器介绍