Java File Manipulation Tools

Source: Internet
Author: User

import java.io.file;/** *  *  @Title  : FileUtils *  @File  name  : FileUtils.java *  @Description  :  file manipulation tools  *  @Date  :  August 10, 2014  *  @author  :  Chinese  *  @version  : 1.0 *  @Others  :  *  @History  1.<br/> *           date : <br/> *          author :  <br/> *          modification: <br/ > */public class fileutils {public static final string file_sep  = system.getproperty ("File.separator");/** *  *  @Function   :   moveFile *  @Desc   :   Mobile directory or file  *  @Author   :  Chinese    *  @param  srcfilepath   source file (directory) path  *  @param  destFilePath   destination file (directory) path  * @ Return  true: Operation succeeded, false: Operation failed  */public static boolean movefile (String  Srcfilepath, string destfilepath)  {file srcfile = new file (SrcFilePath);   (!srcfile.exists ())  {return false;} if  (Srcfile.isdirectory ())  {file[] subfiles = srcfile.listfiles ();for  (File  file : subfiles)  {movefile (File.getabsolutepath (),  destfilepath +file_sep+  file.getname ());}} File destfile = new file (Destfilepath); File destparentfile = destfile.getparentfile ();if  (!destparentfile.exists ())  { Destparentfile.mkdirs ();} Srcfile.renameto (destfile); return true;} /** *  @Function   :  moveChildren *  @Desc   :   Specify all subdirectories and files under directory to destination directory  *  @Author &NBSP;&NBSP;:&NBSP; Chinese    *  @param  srcFilePath   source directory path  *  @param  destfilepath    Destination Path  *  @return   true: Operation succeeded, false: operation failed  */public static boolean  moveallchildren (String srcfilepath, string destfilepath)  {File srcDir =  new file (Srcfilepath);if  (!srcdir.exists ()  | |  !srcdir.isdirectory ())  {return false;} File destdir = new file (Destfilepath);if  (destdir.exists ())  {if  (  Destdir.isfile ())  {destdir.delete ();d estdir = new file (Destfilepath);d estdir.mkdir ();}}  else {destdir.mkdirs ();} File[] children = srcdir.listfiles ();for  (File file : children)  { MoveFile (File.getabsolutepath (),  destfilepath);} Return true;} /** *  *  @Function   :  moveFiles *  @Desc   :    Move the specified file to the destination directory  *  @Author   :  Chinese    *  @param  srcFiles  source files, support multiple path parameters  *   @param  destDirPath  Destination directory  *  @return  true: Operation succeeded, false: operation failed  */public  Static boolean movefiles (string destdirpath, string ... srcfiles) {if  ( srcfiles == null | |  srcfiles.length == 0)  {return false;} File destdir = new file (Destdirpath);boolean destexists = true;if  (! Destdir.exists ())  {destexists = destdir.mkdirs ();  } else if (DestDir.isFile ()) { Return false;} if  (!destexists)  {return false;} for  (string srcfilepath : srcfiles)  {file srcfile = new file ( Srcfilepath);if  (Srcfile.exists ()  && srcfile.isfile ())  {srcfile.renameto (new  File (Destdirpath + file_sep + srcfile.getname ()));}} Return true;}/** *  *  @Function  : deleteSelfAndAllChildren *  @Desc  :  Delete the specified directory and all subdirectories and files below it, and if you specify a file, remove it directly  *  @Author  :  Chinese  *  @param  dirPath  Source directory or file path  *  @return  true: Delete succeeded, false: Delete failed  */public static boolean  Deleteselfandallchildren (String dirpath)  {file srcfile = new file (DirPath);   (!srcfile.exists ())  {return false;} if  (Srcfile.isfile ())  {srcfile.delete ();}  else {file[] children = srcfile.listfiles ();if  (children.length == 0)  {srcfile.delete ();}  else {for  (file file : children)  {//First delete the sub-files and directories Deleteselfandallchildren ( File.getabsolutepath ());} Delete itself Srcfile.delete ();}} Return true;} /** *  *  @Function   :  deleteAllChildren *  @Desc    :   Delete all subdirectories and files under a directory  *  @Author   :  Chinese    *  @param  dirPath *  @return  true: Delete succeeded, false: Delete failed  */ Public static boolean deleteallchildren (String dirpath) {file dir = new  file (Dirpath);if  (!dir.exists ())  {return false;} File[] children = dir.listfiles ();for  (File file : children)  { Deleteselfandallchildren (File.getabsolutepath ());} Return true;}}


Java File Manipulation Tools

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.