/*** Get folder size *@paramFile File instance *@returnLong*/ Public Static Longgetfoldersize (Java.io.File File) {LongSize = 0; Try{java.io.file[] fileList=File.listfiles (); for(inti = 0; i < filelist.length; i++) { if(Filelist[i].isdirectory ()) {size= size +getfoldersize (Filelist[i]); }Else{size= size +filelist[i].length (); } } } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } //return size/1048576; returnsize; } /*** Delete files and directories in the specified directory *@paramDeletethispath *@paramfilepath *@return */ Public voidDeletefolderfile (String FilePath,BooleanDeletethispath) { if(!Textutils.isempty (FilePath)) { Try{File File=NewFile (FilePath); if(File.isdirectory ()) {//Working with CatalogsFile files[] =File.listfiles (); for(inti = 0; i < files.length; i++) {deletefolderfile (Files[i].getabsolutepath (),true); } } if(Deletethispath) {if(!file.isdirectory ()) {//if it is a file, deleteFile.delete (); } Else{//Catalogue if(File.listfiles (). Length = = 0) {//there is no file or directory under the directory, deleteFile.delete (); } } } } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } } /*** Format Unit *@paramSize *@return */ Public StaticString Getformatsize (Doublesize) { Doublekilobyte = size/1024; if(Kilobyte < 1) { returnSize + "Byte (s)"; } Doublemegabyte = kilobyte/1024; if(Megabyte < 1) {BigDecimal result1=NewBigDecimal (double.tostring (kilobyte)); 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"; } Doubleterabytes = gigabyte/1024; if(Terabytes < 1) {BigDecimal RESULT3=NewBigDecimal (double.tostring (gigaByte)); returnResult3.setscale (2, bigdecimal.round_half_up). Toplainstring () + "GB"; } BigDecimal RESULT4=NewBigDecimal (terabytes); returnResult4.setscale (2, bigdecimal.round_half_up). Toplainstring () + "TB"; }
Java---folder and file operations