String manipulation classes and MD5 encryption and decryption classes are commonly used in Java

  • 2020-04-01 03:21:02
  • OfStack

String and MD5 encryption and decryption classes are commonly used in Java

We Java programmers often use utility classes in developing projects. Today I'm going to share two of my utility classes that you can use in your projects.

One, 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;


public class FileUtil {
	private static final String FOLDER_SEPARATOR = "/";
	private static final char EXTENSION_SEPARATOR = '.';

	
	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);
	}

	
	private static void copyPri(File inputFile, File outputFile,
			boolean isOverWrite) throws IOException {
		//It's a file.
		if (inputFile.isFile()) {
			copySimpleFile(inputFile, outputFile, isOverWrite);
		} else {
			//folder
			if (!outputFile.exists()) {
				outputFile.mkdir();
			}
			//Circular subfolder
			for (File child : inputFile.listFiles()) {
				copy(child,
						new File(outputFile.getPath() + "/" + child.getName()),
						isOverWrite);
			}
		}
	}

	
	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 to cover! ");
				}
			} else {
				//No coverage 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();
	}

	
	public static void delete(File file) {
		deleteFile(file);
	}

	
	private static void deleteFile(File file) {
		if (file == null || !file.exists()) {
			return;
		}
		//A single file
		if (!file.isDirectory()) {
			boolean delFlag = file.delete();
			if (!delFlag) {
				throw new RuntimeException(file.getPath() + " Delete failed! ");
			} else {
				return;
			}
		}
		//Delete subdirectory
		for (File child : file.listFiles()) {
			deleteFile(child);
		}
		//Delete his
		file.delete();
	}

	/**
	 *  Extract the file's extension from the file path ,  For example, . "mypath/myfile.txt" -> "txt". * @author  Song Lijun 
	 * 
	 * @date 2014 years 06 month 24 day 
	 * @param  The file path 
	 * @return  if path for null , return directly 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);
	}

	/**
	 *  Extract the file name from the file path ,  Such as:  "mypath/myfile.txt" -> "myfile.txt" .  * @author  Song Lijun 
	 * 
	 * @date 2014 years 06 month 24 day 
	 * @param path
	 *       File path. 
	 * @return  The extracted file name ,  if path for null , return directly 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);
	}

	
	public static void save(byte[] content, File file) throws IOException {
		if (file == null) {
			throw new RuntimeException(" Save file cannot be empty ");
		}
		if (content == null) {
			throw new RuntimeException(" The file stream cannot be empty ");
		}
		InputStream is = new ByteArrayInputStream(content);
		save(is, file);
	}

	
	public static void save(InputStream streamIn, File file) throws IOException {
		if (file == null) {
			throw new RuntimeException(" Save file cannot be empty ");
		}
		if (streamIn == null) {
			throw new RuntimeException(" The file stream cannot be empty ");
		}
		//The output stream
		OutputStream streamOut = null;
		//folder Create if it doesn't 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();
	}
}

Two, MD5 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;


public class FileUtil {
	private static final String FOLDER_SEPARATOR = "/";
	private static final char EXTENSION_SEPARATOR = '.';

	
	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);
	}

	
	private static void copyPri(File inputFile, File outputFile,
			boolean isOverWrite) throws IOException {
		//It's a file.
		if (inputFile.isFile()) {
			copySimpleFile(inputFile, outputFile, isOverWrite);
		} else {
			//folder
			if (!outputFile.exists()) {
				outputFile.mkdir();
			}
			//Circular subfolder
			for (File child : inputFile.listFiles()) {
				copy(child,
						new File(outputFile.getPath() + "/" + child.getName()),
						isOverWrite);
			}
		}
	}

	
	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 to cover! ");
				}
			} else {
				//No coverage 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();
	}

	
	public static void delete(File file) {
		deleteFile(file);
	}

	
	private static void deleteFile(File file) {
		if (file == null || !file.exists()) {
			return;
		}
		//A single file
		if (!file.isDirectory()) {
			boolean delFlag = file.delete();
			if (!delFlag) {
				throw new RuntimeException(file.getPath() + " Delete failed! ");
			} else {
				return;
			}
		}
		//Delete subdirectory
		for (File child : file.listFiles()) {
			deleteFile(child);
		}
		//Delete his
		file.delete();
	}

	/**
	 *  Extract the file's extension from the file path ,  For example, . "mypath/myfile.txt" -> "txt". * @author  Song Lijun 
	 * 
	 * @date 2014 years 06 month 24 day 
	 * @param  The file path 
	 * @return  if path for null , return directly 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);
	}

	/**
	 *  Extract the file name from the file path ,  Such as:  "mypath/myfile.txt" -> "myfile.txt" .  * @author  Song Lijun 
	 * 
	 * @date 2014 years 06 month 24 day 
	 * @param path
	 *       File path. 
	 * @return  The extracted file name ,  if path for null , return directly 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);
	}

	
	public static void save(byte[] content, File file) throws IOException {
		if (file == null) {
			throw new RuntimeException(" Save file cannot be empty ");
		}
		if (content == null) {
			throw new RuntimeException(" The file stream cannot be empty ");
		}
		InputStream is = new ByteArrayInputStream(content);
		save(is, file);
	}

	
	public static void save(InputStream streamIn, File file) throws IOException {
		if (file == null) {
			throw new RuntimeException(" Save file cannot be empty ");
		}
		if (streamIn == null) {
			throw new RuntimeException(" The file stream cannot be empty ");
		}
		//The output stream
		OutputStream streamOut = null;
		//folder Create if it doesn't 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();
	}
}


Related articles: