Java で一般的に使用される文字列および MD5 の暗号化および復号化クラス
私たち Java プログラマーは、プロジェクトを開発するときにいくつかのツール クラスをよく使用します。今日は、プロジェクトで使用できる私のツール クラスを 2 つ共有します。
1. 文字列ツールクラス
パッケージ 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 Song Lijun* @date June 24, 2014*/public class FileUtil {private static Final String FOLDER_SEPARATOR = "/";private static Final char EXTENSION_SEPARATOR = '.';/** * 機能: ファイルまたはファイル フォルダーをコピー。 * * @author Song Lijun* @date June 24, 2014* @param inputFile * ソースファイル* @param OutputFile * コピー先ファイル* @param isOverWrite * 上書きするかどうか(ファイルのみ) * @throws IOException */public static void copy (ファイル入力ファイル、ファイル出力ファイル、ブール値 isOverWrite)throws IOException {if (!inputFile.exists()) {throw new RuntimeException(inputFile.getPath() + "ソース ディレクトリが存在しません!");}copyPri(inputFile, OutputFile, isOverWrite);}/** * 関数: 再帰的に使用します。コピー 。 * * @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 {/ /ファイルです。 if (inputFile.isFile()) {copySimpleFile(inputFile, outputFile, isOverWrite);} else {// Folder if (!outputFile.exists()) {outputFile.mkdir();}// サブフォルダーをループします ( File child : inputFile.listFiles()) {copy(child,new File(outputFile.getPath() + "/" + child.getName()),isOverWrite);}}}/** * 関数: 単一のファイルをコピー* * @author Song Lijun* @date June 24, 2014* @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 Song Lijun* @date 2014 年 6 月 24 日* @param file * file*/public static void delete(File file) {deleteFile(file);}/** * 関数: ファイル削除、内部再帰使用* * @author Song Lijun* @date 2014 June 24, 2016 * @param file * 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;}}// (File child のサブディレクトリを削除します) : file.listFiles()) {deleteFile(child);}// 自分自身を削除 file.delete();}/** * ファイル パスからファイル拡張子を抽出します (例: "mypath/myfile.txt")。 "txt". * @author Song Lijun* * @date June 24, 2014* @param file path* @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;}intfolderIndex = path .lastIndexOf(FOLDER_SEPARATOR);if (folderIndex > extIndex) {return null;}return path.substring(extIndex + 1);}/** * ファイル パスからファイル名を抽出します (例: "mypath/myfile.txt" -> "myfile.txt")。 * @author Song Lijun* * @date June 24, 2014* @param path * ファイルパス。 * @return 抽出されたファイル名が 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 Song Lijun* @date June 24, 2014* @param content * Bytes* @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 Song Lijun* @date June 24 , 2014 * @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("ファイル ストリームを空にすることはできません");}/ /Output Stream OutputStream streamOut = null // フォルダーが存在しない場合は作成します。 if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}streamOut = new FileOutputStream(file);int bytesRead = 0;byte[] バッファ = new byte[8192];while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {streamOut.write(buffer, 0, bytesRead);}streamOut.close();streamIn.close();}}
2. MD5ツール
パッケージ 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 Song Lijun* @date June 24, 2014*/public class FileUtil {private static Final String FOLDER_SEPARATOR = "/";private static Final char EXTENSION_SEPARATOR = '.';/** * 機能: ファイルまたはファイル フォルダーをコピー。 * * @author Song Lijun* @date June 24, 2014* @param inputFile * ソースファイル* @param OutputFile * コピー先ファイル* @param isOverWrite * 上書きするかどうか(ファイルのみ) * @throws IOException */public static void copy (ファイル入力ファイル、ファイル出力ファイル、ブール値 isOverWrite)throws IOException {if (!inputFile.exists()) {throw new RuntimeException(inputFile.getPath() + "ソース ディレクトリが存在しません!");}copyPri(inputFile, OutputFile, isOverWrite);}/** * 関数: 再帰的に使用します。コピー 。 * * @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 {/ /ファイルです。 if (inputFile.isFile()) {copySimpleFile(inputFile, outputFile, isOverWrite);} else {// Folder if (!outputFile.exists()) {outputFile.mkdir();}// サブフォルダーをループします ( File child : inputFile.listFiles()) {copy(child,new File(outputFile.getPath() + "/" + child.getName()),isOverWrite);}}}/** * 関数: 単一のファイルをコピー* * @author Song Lijun* @date June 24, 2014* @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 Song Lijun* @date 2014 年 6 月 24 日* @param file * file*/public static void delete(File file) {deleteFile(file);}/** * 関数: ファイル削除、内部再帰使用* * @author Song Lijun* @date 2014 June 24, 2016 * @param file * 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;}}// (File child のサブディレクトリを削除します) : file.listFiles()) {deleteFile(child);}// 自分自身を削除 file.delete();}/** * ファイル パスからファイル拡張子を抽出します (例: "mypath/myfile.txt")。 "txt". * @author Song Lijun* * @date June 24, 2014* @param file path* @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;}intfolderIndex = path .lastIndexOf(FOLDER_SEPARATOR);if (folderIndex > extIndex) {return null;}return path.substring(extIndex + 1);}/** * ファイル パスからファイル名を抽出します (例: "mypath/myfile.txt" -> "myfile.txt")。 * @author Song Lijun* * @date June 24, 2014* @param path * ファイルパス。 * @return 抽出されたファイル名が 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 Song Lijun* @date June 24, 2014* @param content * Bytes* @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 Song Lijun* @date June 24 , 2014 * @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("ファイル ストリームを空にすることはできません");}/ /Output Stream OutputStream streamOut = null // フォルダーが存在しない場合は作成します。 if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}streamOut = new FileOutputStream(file);int bytesRead = 0;byte[] バッファ = new byte[8192];while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {streamOut.write(buffer, 0, bytesRead);}streamOut.close();streamIn.close();}}