Common tool classes in Java string and MD5 encryption decryption class
We Java programmers in the development of projects are often used in a number of tool classes. Today I share my two tool classes that you can use in your project.
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;
/** * File related operation Auxiliary class.
* * @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 folders. * @author Song Lijun * @date June 24, 2014 * @param inputfile * Source Document * @param outputfile * Purpose Document * @param Isoverwrite * Overwrite (Files only) * @throws IOException/public static void copy (file inputfile, file OutputFile, bo Olean Isoverwrite) throws IOException {if (!inputfile.exists ()) {throw new RuntimeException (Inputfile.getpath)
+ "Source directory does not exist!");
} copypri (Inputfile, outputfile, isoverwrite);
/** * Function: for copy to do recursively use. * * @author Song Lijun * @date June 24, 2014 * @param inputfile * @param outputfiLe * @param isoverwrite * @throws ioexception/private static void Copypri (file inputfile, file OutputFile, bo
Olean Isoverwrite) throws IOException {//is a file.
if (Inputfile.isfile ()) {Copysimplefile (Inputfile, outputfile, isoverwrite);
else {//folder if (!outputfile.exists ()) {Outputfile.mkdir (); ///Loop subfolder for (File child:inputFile.listFiles ()) {Copy (child, New File (Outputfile.getpath () + "/" + C
Hild.getname ()), isoverwrite); /** * Features: Copy single file * * @author Song Lijun * @date June 24, 2014 * @param inputfile * source file * @param out Putfile * Target file * @param isoverwrite * is allowed to overwrite * @throws IOException/private static void Copysimplef Ile (file inputfile, File OutputFile, Boolean isoverwrite) throws IOException {//destination file already exists if (outputfile.exists ()) {if (isoverwrite) {if (!outputfile.delete ()) {throw new RuntimeException (Outputfile.getpath () + "cannot overwrite!)
"); } else {//not allowed to overwrite 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 (); /** * Feature: Delete file * * @author Song Lijun * @date June 24, 2014 * @param file */public static void Delete (File file)
{DeleteFile (file); /** * Function: Delete file, internal recursive use * * @author Song Lijun * @date June 24, 2014 * @param file * @return Boolean True
Delete succeeded, false deletion failed.
* * private static void DeleteFile (file File = = NULL | |!file.exists ()) {return;
///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 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, NULL is returned 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); /** * Extracts the file name from the file path, for example: "Mypath/myfile.txt"-> "MyFile.txt".
* @author Song Lijun * * @date June 24, 2014 * @param path * file paths.
* @return The extracted file name, if path is NULL, return null 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 file. * * @author Song Lijun * @date June 24, 2014 * @param Content * Bytes * @param file * saved to files * @throws ioexception/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 ("file stream cannot be empty");
} InputStream is = new Bytearrayinputstream (content);
Save (is, file); /** * Features: Save file * * @author Song Lijun * @date June 24, 2014 * @param streamin * File stream * @param files * Security Saved to File * @throws IOException/public static void Save (InputStream streamin, File file) throws IOException {if (fi
Le = = null) {throw new RuntimeException ("Save file cannot be empty");
} if (streamin = = null) {throw new RuntimeException ("file stream cannot be empty");
}//output stream outputstream streamout = null;
Create a 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 (); }
}
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;
/** * File related operation Auxiliary class.
* * @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 folders. * @author Song Lijun * @date June 24, 2014 * @param inputfile * Source Document * @param outputfile * Purpose Document * @param Isoverwrite * Overwrite (Files only) * @throws IOException/public static void copy (file inputfile, file OutputFile, bo Olean Isoverwrite) throws IOException {if (!inputfile.exists ()) {throw new RuntimeException (Inputfile.getpath)
+ "Source directory does not exist!");
} copypri (Inputfile, outputfile, isoverwrite);
/** * Function: for copy to do recursively use. * * @author Song Lijun * @date June 24, 2014 * @param inputfile * @param outputfiLe * @param isoverwrite * @throws ioexception/private static void Copypri (file inputfile, file OutputFile, bo
Olean Isoverwrite) throws IOException {//is a file.
if (Inputfile.isfile ()) {Copysimplefile (Inputfile, outputfile, isoverwrite);
else {//folder if (!outputfile.exists ()) {Outputfile.mkdir (); ///Loop subfolder for (File child:inputFile.listFiles ()) {Copy (child, New File (Outputfile.getpath () + "/" + C
Hild.getname ()), isoverwrite); /** * Features: Copy single file * * @author Song Lijun * @date June 24, 2014 * @param inputfile * source file * @param out Putfile * Target file * @param isoverwrite * is allowed to overwrite * @throws IOException/private static void Copysimplef Ile (file inputfile, File OutputFile, Boolean isoverwrite) throws IOException {//destination file already exists if (outputfile.exists ()) {if (isoverwrite) {if (!outputfile.delete ()) {throw new RuntimeException (Outputfile.getpath () + "cannot overwrite!)
"); } else {//not allowed to overwrite 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 (); /** * Feature: Delete file * * @author Song Lijun * @date June 24, 2014 * @param file */public static void Delete (File file)
{DeleteFile (file); /** * Function: Delete file, internal recursive use * * @author Song Lijun * @date June 24, 2014 * @param file * @return Boolean True
Delete succeeded, false deletion failed.
* * private static void DeleteFile (file File = = NULL | |!file.exists ()) {return;
///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 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, NULL is returned 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); /** * Extracts the file name from the file path, for example: "Mypath/myfile.txt"-> "MyFile.txt".
* @author Song Lijun * * @date June 24, 2014 * @param path * file paths.
* @return The extracted file name, if path is NULL, return null 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 file. * * @author Song Lijun * @date June 24, 2014 * @param Content * Bytes * @param file * saved to files * @throws ioexception/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 ("file stream cannot be empty");
} InputStream is = new Bytearrayinputstream (content);
Save (is, file); /** * Features: Save file * * @author Song Lijun * @date June 24, 2014 * @param streamin * File stream * @param files * Security Saved to File * @throws IOException/public static void Save (InputStream streamin, File file) throws IOException {if (fi
Le = = null) {throw new RuntimeException ("Save file cannot be empty");
} if (streamin = = null) {throw new RuntimeException ("file stream cannot be empty");
}//output stream outputstream streamout = null;
Create a 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 (); }
}