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