String and MD5 encryption and decryption classes commonly used in Java
We Java programmers often use some tool classes when developing projects. Today I will share two of my tool classes that you can use in your projects.
1. String tool class
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;/** * Auxiliary class for file related operations. * * @author Song Lijun* @date June 24, 2014*/public class FileUtil {private static final String FOLDER_SEPARATOR = "/";private static final char EXTENSION_SEPARATOR = '.';/** * Function: copy files or files folder. * * @author Song Lijun* @date June 24, 2014* @param inputFile * Source file* @param outputFile * Destination file* @param isOverWrite * Whether to overwrite (only for files) * @throws IOException */public static void copy (File inputFile, File outputFile, boolean isOverWrite)throws IOException {if (!inputFile.exists()) {throw new RuntimeException(inputFile.getPath() + "The source directory does not exist!");}copyPri(inputFile, outputFile, isOverWrite);}/** * Function: Recursive use for copy . * * @author Song Lijun* @date June 24, 2014* @param inputFile * @param outputFile * @param isOverWrite * @throws IOException */private static void copyPri(File inputFile, File outputFile,boolean isOverWrite) throws IOException {/ / is a file. if (inputFile.isFile()) {copySimpleFile(inputFile, outputFile, isOverWrite);} else {// Folder if (!outputFile.exists()) {outputFile.mkdir();}// Loop subfolders for ( File child : inputFile.listFiles()) {copy(child,new File(outputFile.getPath() + "/" + child.getName()),isOverWrite);}}}/** * Function: copy a single file* * @author Song Lijun* @date June 24, 2014* @param inputFile * source file* @param outputFile * target file * @param isOverWrite * Whether overwriting is allowed * @throws IOException */private static void copySimpleFile(File inputFile, File outputFile,boolean isOverWrite) throws IOException {//The target file already exists if (outputFile.exists()) {if (isOverWrite) {if (!outputFile.delete()) {throw new RuntimeException(outputFile.getPath() + "Unable Overwrite! ");}} else {// Overwriting is not allowed 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();}/** * Function: delete files* * @author Song Lijun* @date June 24, 2014* @param file * file*/public static void delete(File file) {deleteFile(file);}/** * Function: delete file, internal recursive use* * @author Song Lijun* @date 2014 June 24, 2016 * @param file * file * @return boolean true if the deletion is successful, false if the deletion fails. */private static void deleteFile(File file) {if (file == null || !file.exists()) {return;}//Single file if (!file.isDirectory()) {boolean delFlag = file.delete ();if (!delFlag) {throw new RuntimeException(file.getPath() + "Deletion failed!");} else {return;}}// Delete subdirectory for (File child : file.listFiles()) {deleteFile(child);}// Delete yourself file.delete();}/** * Extract the file extension from the file path, for example. "mypath/myfile.txt" -> "txt". * @author Song Lijun* * @date June 24, 2014* @param file path* @return If path is null, return null directly. */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);}/** * Extract the file name from the file path, for example: "mypath/myfile.txt" -> "myfile.txt". * @author Song Lijun* * @date June 24, 2014* @param path * File path. * @return The extracted file name. If path is null, null will be returned directly. */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);}/** * Function: Save the file. * * @author Song Lijun* @date June 24, 2014* @param content * Bytes* @param file * File saved to * @throws IOException */public static void save(byte[] content, File file) throws IOException {if (file == null) {throw new RuntimeException("The saved file cannot be empty");} if (content == null) {throw new RuntimeException("The file stream cannot be empty");}InputStream is = new ByteArrayInputStream(content);save(is, file);}/** * Function: Save the file* * @author Song Lijun* @date June 24, 2014 * @param streamIn * File stream * @param file * File saved to * @throws IOException */public static void save(InputStream streamIn, File file) throws IOException {if (file == null) {throw new RuntimeException("The saved file cannot be empty");} if (streamIn == null) {throw new RuntimeException("The file stream cannot be empty");}//Output Stream OutputStream streamOut = null; //Create the folder if it does not exist. 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();}}
2. MD5 tools
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;/** * Auxiliary class for file related operations. * * @author Song Lijun* @date June 24, 2014*/public class FileUtil {private static final String FOLDER_SEPARATOR = "/";private static final char EXTENSION_SEPARATOR = '.';/** * Function: copy files or files folder. * * @author Song Lijun* @date June 24, 2014* @param inputFile * Source file* @param outputFile * Destination file* @param isOverWrite * Whether to overwrite (only for files) * @throws IOException */public static void copy (File inputFile, File outputFile, boolean isOverWrite)throws IOException {if (!inputFile.exists()) {throw new RuntimeException(inputFile.getPath() + "The source directory does not exist!");}copyPri(inputFile, outputFile, isOverWrite);}/** * Function: Recursive use for copy . * * @author Song Lijun* @date June 24, 2014* @param inputFile * @param outputFile * @param isOverWrite * @throws IOException */private static void copyPri(File inputFile, File outputFile,boolean isOverWrite) throws IOException {/ / is a file. if (inputFile.isFile()) {copySimpleFile(inputFile, outputFile, isOverWrite);} else {// Folder if (!outputFile.exists()) {outputFile.mkdir();}// Loop subfolders for ( File child : inputFile.listFiles()) {copy(child,new File(outputFile.getPath() + "/" + child.getName()),isOverWrite);}}}/** * Function: copy a single file* * @author Song Lijun* @date June 24, 2014* @param inputFile * source file* @param outputFile * target file * @param isOverWrite * Whether overwriting is allowed * @throws IOException */private static void copySimpleFile(File inputFile, File outputFile,boolean isOverWrite) throws IOException {//The target file already exists if (outputFile.exists()) {if (isOverWrite) {if (!outputFile.delete()) {throw new RuntimeException(outputFile.getPath() + "Unable Overwrite! ");}} else {// Overwriting is not allowed 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();}/** * Function: delete files* * @author Song Lijun* @date June 24, 2014* @param file * file*/public static void delete(File file) {deleteFile(file);}/** * Function: delete file, internal recursive use* * @author Song Lijun* @date 2014 June 24, 2016 * @param file * file * @return boolean true if the deletion is successful, false if the deletion fails. */private static void deleteFile(File file) {if (file == null || !file.exists()) {return;}//Single file if (!file.isDirectory()) {boolean delFlag = file.delete ();if (!delFlag) {throw new RuntimeException(file.getPath() + "Deletion failed!");} else {return;}}// Delete subdirectory for (File child : file.listFiles()) {deleteFile(child);}// Delete yourself file.delete();}/** * Extract the file extension from the file path, for example. "mypath/myfile.txt" -> "txt". * @author Song Lijun* * @date June 24, 2014* @param file path* @return If path is null, return null directly. */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);}/** * Extract the file name from the file path, for example: "mypath/myfile.txt" -> "myfile.txt". * @author Song Lijun* * @date June 24, 2014* @param path * File path. * @return The extracted file name. If path is null, null will be returned directly. */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);}/** * Function: Save the file. * * @author Song Lijun* @date June 24, 2014* @param content * Bytes* @param file * File saved to * @throws IOException */public static void save(byte[] content, File file) throws IOException {if (file == null) {throw new RuntimeException("The saved file cannot be empty");} if (content == null) {throw new RuntimeException("The file stream cannot be empty");}InputStream is = new ByteArrayInputStream(content);save(is, file);}/** * Function: Save the file* * @author Song Lijun* @date June 24, 2014 * @param streamIn * File stream * @param file * File saved to * @throws IOException */public static void save(InputStream streamIn, File file) throws IOException {if (file == null) {throw new RuntimeException("The saved file cannot be empty");} if (streamIn == null) {throw new RuntimeException("The file stream cannot be empty");}//Output Stream OutputStream streamOut = null; //Create the folder if it does not exist. 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();}}