java中經常使用對象類之字符串操作類和MD5加密解密類。本站提示廣大學習愛好者:(java中經常使用對象類之字符串操作類和MD5加密解密類)文章只能為提供參考,不一定能成為您想要的結果。以下是java中經常使用對象類之字符串操作類和MD5加密解密類正文
java中經常使用的對象類之String和MD5加密解密類
我們java法式員在開辟項目標是經常會用到一些對象類。明天我分享一下我的兩個對象類,年夜家可以在項目中應用。
1、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();
}
}
2、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();
}
}