How to create a directory and delete a directory on disk in a Java program

Source: Internet
Author: User
Import   java.io.*;     import   java.util.*;         public   class   Fileoperator     {          &NB Sp Public   Fileoperator ()   { }                     &NBSP ;  /**             *     features:   Mobile files (only mobile files)             *     parameters:   STRSOURCEFILENAME: Specified file full path name             *   & nbsp                 Strdestdir:               move to specified Folder             *     return value:   If successful true; otherwise, false         &N Bsp   *             public   Boolean   CopyTo (String   Strsourcefilename, & nbsp                                                  &NB Sp     String   Strdestdir)             {          &NB Sp         File   Filesource   =   new   file (strsourcefilename);                     File   filedest       =   N EW   File (STRDESTDIR);                                   &NB Sp    //  If source files are not saved or source files are folders                     if   (! Filesource.exists ()   | |  !filesource.isfile ())   {                            SYSTEM.OUT.PRINTLN ("Error:   Fileoperator.java   CopyTo function,/n reason:   source file["  +   Strsourcefilename   +  "], no presence or folder! ");                             return   false;                                   & nbsp                        //  If destination folder does not exist     &NB Sp               if   (!filedest.isdirectory ()   | |  !filedest.exists ()) & nbsp {                            if   (!FILEDEST.M Kdirs ())   {                                    SYSTEM.OUT.PRINTLN ("Error:   Fileoperator.java   CopyTo function,/n Reason: Directory folder does not exist, failed to create target folder!");                                    return   false;                            }       & nbsp                                   & nbsp                 Try   {              &NBS P             String   strabsfilename   =   Strdestdir   +   FILE.SEP Arator   +   filesource.getname ();                                   &NB Sp                     FileInputStream           F Ileinput     =   new   FileInputStream (strsourcefilename);                             FileOutputStream         fileoutput   =   New   FileOutputStream (strabsfilename);                                   &NB Sp                    //SYSTEM.OUT.PRINTLN ("Start copying file:");                                   &NB Sp                     int           I   &NBS P                   =   0;                             int           Count               =  -1;                     &NBSp                                   LON G         nwritesize     =   0;                             long     &NBSP ;   nfilesize       =   filesource.length ();                                   &NB Sp                     byte[]     data       &NBSP ;         =   new   Byte[buffer];                                   &NB Sp                    //SYSTEM.OUT.PRINTLN ("FILE   NAME  /T/T & nbsp FILE   SIZE ");         &nbsp                   SYSTEM.OUT.PRINTLN (Filesource.getname ()   +   "/t /T "  +   nfilesize   +  "   byte ");                                   &NB Sp                     while   ( -1  !=   (count   = &N Bsp Fileinput.read (data,   0,   BUFFER))   {                  &N Bsp                                   &NB Sp             fileoutput.write (data,   0,   count);                                   &NB Sp                         &NBSp           nwritesize   + =   count;                                   &NB Sp                                   &NB Sp Long   size       =   (nwritesize   *  )/nfilesize;                                   &NB Sp Long   T             =   nwritesize;                                   &NB Sp                                   &NB Sp String   MSG   =   NULL;                         &NBSP                                   &NB Sp           If   (size   <=     &&   size   >= &nbs P 0   {                              &NBSP ;             MSG   =   "/R Copy File progress:  "   +   size   +  %  /T "  +  "/  has been copied:   "  +  ";                                   &NB Sp         System.out.print (msg);                                   &NB Sp }   Else   if   (size   >  )   {              &NB Sp                            MSG   =   "/R copy File progress:   "  +     +  "%  /T "  +  "/  copy:   "  +   T                                   &NB Sp         System.out.print (msg);                                   &NB Sp }                                   & nbsp                                   & nbsp                                   &NB Sp         fileinput.close ();                             fileoutput.close ();                                   &NB Sp                     SYSTEM.OUT.PRINTLN ("/n Copy File Success!");                             return   true;                                   &NB Sp            }   catch (Exception   e)   {  &NBSP;SYSTEM.OUT.PRINTLN ("Abnormal letter") Interest: [");                             e.printstacktrace ();                             SYSTEM.OUT.PRINTLN ("Exception information :]");               &NBSP             return   false;                     {           }                        /**           &NBSP ; *   Work     can:   Delete specified file             *   parameter     Number:   designation Absolute path filename   strFileName             *   return value:   If Delete succeeds true otherwise false;            /            public   Boolean   Delete (String   strFileName)             {              & nbsp     file   filedelete   =   new   File (strFileName);                                   &NB SP     If   (!filedelete.exists ()   | |  !filedelete.isfile ())   {                            SYSTEM.OUT.PRINTLN ("Error:  "   +   STR FileName   +   "does not exist!");                             return   false;                                   & nbsp                         return   filedelete.delete ();                                   & nbsp /**               *     features:   Mobile files (only mobile files)               *     parameters:   Strsourcefilename:     is the specified file full path name   &NBSp           *                     Strdestdir: &NB Sp                 move to specified folder               * &nbs P   Return value:   If success is true;               Otherwise false              /            public   Boolean   moveFile (String   strsourcefilename,       & nbsp                                   &NB Sp                   String   Strdestdir)         &NBSP ;   {                    if   (CopyTo strsourcefilename,   Strdestdir)                       &NBSp     return   this.delete (strsourcefilename);                     else             &NBSP ;               return   false;                                   & nbsp /**               *     function:   Create folder               *     parameters:   strdir             The folder name to be created               *     return value:   If successful true; otherwise false      ,         *             public   Boolean   MakeDir (String   strdir)     &NBS P       {                    File   filenew   = &nbsp New   File (Strdir);                                   &NB Sp     If   (!filenew.exists ())                       & nbsp     return   filenew.mkdirs ();                     else             &NBSP ;               return   true;                                   & nbsp /**               *     function:   Delete folder               *     parameters:   strdir             The folder name to delete               *     return value:   If success true; false       &NB  Sp     *             public   Boolean   rmdir (String   strdir)             {                    File   RM Dir   =   new   File (Strdir);                     if   (Rmdir.isdirectory ()   && &NB Sp Rmdir.exists ())   {                            String[]   filelist       =   rmdir.list ();                                   &NB Sp                     int             Size & nbsp             =   Filelist.length;                   &NBSP         for   (int   i   =   0;   I   <   filelist.length; &nbsp ; i++)   {                              &NB Sp     String     subfile   =   Strdir   +   File.separator   +   filelist[ I];                                   &NB Sp File         TMP           =   new   File (subfile);                                   &NB Sp If   (Tmp.isfile ())                       tmp.delete ();                                   &NB Sp   Else   IF   (Tmp.isdirectory ())   rmdir (subfile);                                   &NB Sp else                                 &NBSP ;               SYSTEM.OUT.PRINTLN ("error!");                            }       & nbsp                     rmdir.delete ();                     {  Else   return   false;                     return   true;                                   & nbsp        //  member   variable                        private   final   int   BUFFER   =   1024;                        //  member   variable &nbsp ;           class   Test     {            public &N Bsp static   void   Main (string[]   args)             {      &NBS P        }    
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.