Java file operation Enhancement Tool

Source: Internet
Author: User
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;
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.