"Android Notes" Android Stats folder size, delete all file methods under folder

Source: Internet
Author: User

1. Get the folder size:

1 /**   2 * Get folder size3      * @paramFile File Instance4      * @returnLong5      */     6      Public Static Longgetfoldersize (Java.io.File File) {7    8         LongSize = 0; 9         Try {  Tenjava.io.file[] FileList =File.listfiles ();  One              for(inti = 0; i < filelist.length; i++)      A             {      -                 if(Filelist[i].isdirectory ()) -                 {      theSize = size +getfoldersize (Filelist[i]);  -     -}Else{      -Size = size +filelist[i].length ();  +     -                 }      +             }   A}Catch(Exception e) { at             //TODO auto-generated Catch block - E.printstacktrace ();  -         }      -        //return size/1048576;  -         returnsize;  -}

2. Delete all files under folder:

1 /**   2 * Delete files and directories in the specified directory3      * @paramDeletethispath4      * @paramfilepath5      * @return    6      */     7      Public voidDeletefolderfile (String FilePath,BooleanDeletethispath) {     8         if(!Textutils.isempty (FilePath)) {     9             Try {  TenFile File =NewFile (FilePath);  One                 if(File.isdirectory ()) {//Working with Catalogs AFile files[] =File.listfiles ();  -                      for(inti = 0; i < files.length; i++) {      -Deletefolderfile (Files[i].getabsolutepath (),true);  the                     }       -                 }      -                 if(Deletethispath) { -                     if(!file.isdirectory ()) {//if it is a file, delete + File.delete ();  -}Else{//Catalogue +                    if(File.listfiles (). Length = = 0) {//there is no file or directory under the directory, delete A File.delete ();  at                         }      -                     }      -                 }   -}Catch(Exception e) { -                 //TODO auto-generated Catch block - E.printstacktrace ();  in             }      -         }      to}

3. Format Folder size unit:

1 /** 2 * Formatted units3      * @paramsize4      * @return 5      */  6      Public StaticString Getformatsize (Doublesize) {  7         Doublekilobyte = size/1024; 8         if(Kilobyte < 1) {  9             returnSize + "B"; Ten         }   One            A         Doublemegabyte = kilobyte/1024;  -         if(Megabyte < 1) {   -BigDecimal RESULT1 =NewBigDecimal (double.tostring (kilobyte));  the             returnResult1.setscale (2, bigdecimal.round_half_up). Toplainstring () + "KB";  -         }   -            -         DoubleGigaByte = megabyte/1024;  +         if(GigaByte < 1) {   -BigDecimal RESULT2 =NewBigDecimal (double.tostring (megabyte));  +             returnResult2.setscale (2, bigdecimal.round_half_up). Toplainstring () + "MB";  A         }   at            -         Doubleterabytes = gigabyte/1024;  -         if(Terabytes < 1) {   -BigDecimal RESULT3 =NewBigDecimal (double.tostring (gigaByte));  -             returnResult3.setscale (2, bigdecimal.round_half_up). Toplainstring () + "GB";  -         }   inBigDecimal RESULT4 =NewBigDecimal (terabytes);  -         returnResult4.setscale (2, bigdecimal.round_half_up). Toplainstring () + "TB";  to}

"Android Notes" Android Stats folder size, delete all file methods under folder

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.