在java程序中怎样在磁盘上创建目录和删除目录

来源:互联网 发布:行政办公审批软件 编辑:程序博客网 时间:2024/06/04 17:41
import   java.io.*;     import   java.util.*;         public   class   FileOperator     {             public   FileOperator()   {   }                         /**             *     功 能:   移动文件(只能移动文件)             *     参 数:   strSourceFileName:指定的文件全路径名             *                     strDestDir:               移动到指定的文件夹             *     返回值:   如果成功true;否则false             */             public   boolean   copyTo(String   strSourceFileName,                                                           String   strDestDir)             {                     File   fileSource   =   new   File(strSourceFileName);                     File   fileDest       =   new   File(strDestDir);                                         //   如果源文件不存或源文件是文件夹                     if   (!fileSource.exists()   ||   !fileSource.isFile())   {                             System.out.println("错误:   FileOperator.java   copyTo函数,/n原因:   源文件["   +   strSourceFileName   +   "],不存在或是文件夹!");                             return   false;                     }                                         //   如果目标文件夹不存在                     if   (!fileDest.isDirectory()   ||   !fileDest.exists())   {                             if   (!fileDest.mkdirs())   {                                     System.out.println("错误:   FileOperator.java   copyTo函数,/n原因:目录文件夹不存,在创建目标文件夹时失败!");                                     return   false;                             }                     }                                         try   {                             String   strAbsFilename   =   strDestDir   +   File.separator   +   fileSource.getName();                                                         FileInputStream           fileInput     =   new   FileInputStream(strSourceFileName);                             FileOutputStream         fileOutput   =   new   FileOutputStream(strAbsFilename);                                                         //System.out.println("开始拷贝文件:");                                                         int           i                       =   0;                             int           count               =   -1;                                                         long         nWriteSize     =   0;                             long         nFileSize       =   fileSource.length();                                                         byte[]     data                 =   new   byte[BUFFER];                                                         //System.out.println("FILE   NAME   /t/t   FILE   SIZE");                             System.out.println(fileSource.getName()   +   "/t/t"   +   nFileSize   +   "   byte");                                                         while   (-1   !=   (count   =   fileInput.read(data,   0,   BUFFER)))   {                                                                     fileOutput.write(data,   0,   count);                                                                         nWriteSize   +=   count;                                                                         long   size       =   (nWriteSize   *   100)/nFileSize;                                     long   t             =   nWriteSize;                                                                         String   msg   =   null;                                                                         if   (size   <=   100   &&   size   >=   0)   {                                             msg   =   "/r拷贝文件进度:   "   +   size   +   "%   /t"   +   "/t   已拷贝:   "   +   t;                                             System.out.print(msg);                                     }   else   if   (size   >   100)   {                                             msg   =   "/r拷贝文件进度:   "   +   100   +   "%   /t"   +   "/t   已拷贝:   "   +   t;                                             System.out.print(msg);                                     }                                                             }                                                         fileInput.close();                             fileOutput.close();                                                         System.out.println("/n拷贝文件成功!");                             return   true;                                                 }   catch(Exception   e)   {    System.out.println("异常信息:[");                             e.printStackTrace();                             System.out.println("异常信息:]");                             return   false;                     }             }                         /**             *   功     能:   删除指定的文件             *   参     数:   指定绝对路径的文件名   strFileName             *   返回值:   如果删除成功true否则false;             */             public   boolean   delete(String   strFileName)             {                     File   fileDelete   =   new   File(strFileName);                                         if   (!fileDelete.exists()   ||   !fileDelete.isFile())   {                             System.out.println("错误:   "   +   strFileName   +   "不存在!");                             return   false;                     }                                         return   fileDelete.delete();             }                         /**               *     功 能:   移动文件(只能移动文件)               *     参 数:   strSourceFileName:     是指定的文件全路径名               *                     strDestDir:                   移动到指定的文件夹中               *     返回值:   如果成功true;               否则false               */             public   boolean   moveFile(String   strSourceFileName,                                                               String   strDestDir)             {                     if   (copyTo(strSourceFileName,   strDestDir))                             return   this.delete(strSourceFileName);                     else                             return   false;             }                         /**               *     功 能:   创建文件夹               *     参 数:   strDir             要创建的文件夹名称               *     返回值:   如果成功true;否则false               */             public   boolean   makedir(String   strDir)             {                     File   fileNew   =   new   File(strDir);                                         if   (!fileNew.exists())                             return   fileNew.mkdirs();                     else                             return   true;             }                         /**               *     功 能:   删除文件夹               *     参 数:   strDir             要删除的文件夹名称               *     返回值:   如果成功true;否则false               */             public   boolean   rmdir(String   strDir)             {                     File   rmDir   =   new   File(strDir);                     if   (rmDir.isDirectory()   &&   rmDir.exists())   {                             String[]   fileList       =   rmDir.list();                                                         int             size               =   fileList.length;                             for   (int   i   =   0;   i   <   fileList.length;   i++)   {                                     String     subFile   =   strDir   +   File.separator   +   fileList[i];                                     File         tmp           =   new   File(subFile);                                     if   (tmp.isFile())                       tmp.delete();                                       else   if   (tmp.isDirectory())   rmdir(subFile);                                     else                                                 System.out.println("error!");                             }                             rmDir.delete();                     }   else   return   false;                     return   true;             }                                 //   member   variable                         private   final   int   BUFFER   =   1024;                         //   member   variable     }         class   Test     {             public   static   void   main(String[]   args)             {                 }     }
原创粉丝点击