Java file operations are too basic
Fliggy biography, Lack of many practical tools, such as Directory operations, the support is very poor. If you often use Java to operate on files or folders, you will find it frustrating to write these code repeatedly and require a lot of recursion. The following is a solution. The Apache commons I/O toolkit is used to copy, move, delete, and obtain the size of files (CLIPS) without strict tests, if any problem is found, please leave a message to me. Package zzvcom. CMS. ccm. commons;
Import org. Apache. commons. Io. fileutils;
Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory;
Import java. Io. file;
Import java. Io. ioexception;
/**
* File toolbox
*
* @ Author leizhimin 21:12:58
*/
Public final class filetookit {
Private Static final log = logfactory. getlog (filetookit. Class );
/**
* Copy files or directories. The files are identical before and after copying.
*
* @ Param resfilepath: source file path
* @ Param distfolder target folder
* @ Ioexception thrown when an exception occurs in the operation
*/
Public static void copyfile (string resfilepath, string distfolder) throws ioexception {
File resfile = new file (resfilepath );
File distfile = new file (distfolder );
If (resfile. isdirectory ()){
Fileutils. copydirectorytodirectory (resfile, distfile );
} Else if (resfile. isfile ()){
Fileutils. copyfiletodirectory (resfile, distfile, true );
}
}
/**
* Delete a file or directory
*
* @ Param targetpath file or directory path
* @ Ioexception thrown when an exception occurs in the operation
*/
Public static void deletefile (string targetpath) throws ioexception {
File targetfile = new file (targetpath );
If (targetfile. isdirectory ()){
Fileutils. deletedirectory (targetfile );
} Else if (targetfile. isfile ()){
Targetfile. Delete ();
}
}
/**
* Move a file or directory. The files are identical before and after the folder is moved. If the target Folder does not exist, the file is created.
*
* @ Param resfilepath: source file path
* @ Param distfolder target folder
* @ Ioexception thrown when an exception occurs in the operation
*/
Public static void movefile (string resfilepath, string distfolder) throws ioexception {
File resfile = new file (resfilepath );
File distfile = new file (distfolder );
If (resfile. isdirectory ()){
Fileutils. movedirectorytodirectory (resfile, distfile, true );
} Else if (resfile. isfile ()){
Fileutils. movefiletodirectory (resfile, distfile, true );
}
}
/**
* Rename a file or folder
*
* @ Param resfilepath: source file path
* @ Param newfilename rename
* @ Return indicates a successful operation.
*/
Public static Boolean renamefile (string resfilepath, string newfilename ){
String parentfilepath = new file (resfilepath). getparent ();
String newfilepath = stringtookit. formatpath (parentfilepath + "/" + newfilename );
File resfile = new file (resfilepath );
File newfile = new file (newfilepath );
Return resfile. renameto (newfile );
}
/**
* Size of the file or directory to be read
*
* @ Param distfilepath: target file or folder
* @ Return the size of the file or directory. If the file fails to be retrieved,-1 is returned.
*/
Public static long genfilesize (string distfilepath ){
File distfile = new file (distfilepath );
If (distfile. isfile ()){
Return distfile. Length ();
} Else if (distfile. isdirectory ()){
Return fileutils. sizeofdirectory (distfile );
}
Return-1l;
}
}