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中常用工具類之字串操作類和MD5加密解密類相關文章請關注PHP中文網!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)